findRecords
(Since 1.0.0) -- Query items from the database.
function findRecords($params) // Pods 1.9.2+
function findRecords($orderby = 't.id DESC', $rows_per_page = 15, $where = null, $sql = null)
The findRecords method queries for Pod items in the database. Use the showTemplate or fetchRecord functions to fetch the DB results.
Always use pods_sanitize() on any variables which are user-defined (unless you're using your own SQL query, which you'll want to sanitize the various variables there). Examples of usage are like in your 'where' statement "t.name = '$name'", be sure to pods_sanitize($name) before it's use, or if you're already using pods_url_variable() for any variables, it will automatically sanitize for you.
Parameters
| Parameter | Type | Details | ||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| $params | ARRAY | An array of itemssort the items (prefix pod columns with "t." and pick columns with "column_name_here.") | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
| Parameter | Type | Default | Details |
|---|---|---|---|
| $orderby | STRING | t.id | sort the items (prefix pod columns with "t." and pick columns with "column_name_here.") |
| $rows_per_page | INT | 15 | the number of items per page, set to -1 for no limit |
| $where | STRING | null | only show items that meet a certain criteria (prefix pod columns with "t." and pick columns with "column_name_here.") |
| $sql | STRING | null | use your own SQL SELECT statement (advanced) |
Column Prefixes are Important
Columns can be referenced for both non-PICK columns and PICK Columns in the $orderby and $where parameters.
- To reference a non-PICK column, prefix the column name with "t." (e.g. "t.amount")
- To reference PICK column, use the column_name[dot]related_column_name (e.g. "person.first_name")
Returns
NothingExamples
<?php
// SIMPLE SYNTAX
$ducks = new Pod('duck');
$ducks->findRecords('breed.name,t.name',-1,'t.other_column=1');
// Customizing the 'select' here so that no extra data is pulled in that I don't need (SQL query optimization)
$bunnies = new Pod('bunny');
$params = array('select'=>'t.name','orderby'=>'t.name','limit'=>25);
$bunnies->findRecords($params);
// Example of all $params options
$params = array();
$params['select'] = 't.name,my_custom_join.coolness';
$params['join'] = ' LEFT JOIN wp_custom_joined_table AS my_custom_join ON my_custom_join.column_there = t.column_here ';
$params['search'] = false;
$params['where'] = 'my_custom_join.coolness LIKE "%awesome%"';
$params['groupby'] = 't.shared_value';
$params['orderby'] = 't.name';
$params['page'] = 2;
$params['limit'] = 25;
// Or override it all with any SELECT query you want
$params['sql'] = 'SELECT SQL_CALC_FOUND_ROWS * FROM my_other_table';
