findRecords
(Since 1.0.0) -- Query items from the database.
$pod->findRecords($params) // Pods 1.9.2+
$pod = new Pod('pod_name', $params) // Pods 1.12+
$pod->findRecords($orderby, $rows_per_page, $where, $sql) // Deprecated Style
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
// If you supply an array to the Pod() object, it will run findRecords for you!
$params = array('where'=>'t.id != 4','orderby'=>'t.name','limit'=>25);
$bunnies = new Pod('bunny', $params);
// 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);
// SIMPLE SYNTAX (deprecated)
$ducks = new Pod('duck');
$ducks->findRecords('breed.name,t.name',-1,'t.other_column=1');
// 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['pagination'] = false; // disable pagination
$params['page_var'] = 'pg2'; // custom page variable (Pods 1.12.4+)
$params['search'] = false; // disable search
$params['limit'] = 25;
$params['offset'] = 5;
// Or override it all with any SELECT query you want
$params['sql'] = 'SELECT SQL_CALC_FOUND_ROWS * FROM my_other_table';
// Additional vars on Pod() object
$bunnies->page_var = 'pg2'; // custom pagination var to look for (default is 'pg')
$bunnies->page = 4; // manually set page number for whole object
$bunnies->pagination = false; // disable pagination for whole object
$bunnies->search_var = 'search2'; // custom search var to look for (default is 'search')
$bunnies->search = false; // disable search for whole object
// Traverse multiple layers deep within relationships (added Pods 1.12+)
$params['where'] = 'parent.category.name = "Downloads"';
