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.")
$params Parameter Type Default Details
select STRING t.*, p.id AS pod_id,
p.created, p.modified
for faster SQL processing only pull the columns you need in the list used by findRecords
join STRING null add custom SQL table joins to your query without having to write an entire query (advanced)
search boolean true allow Pod::getFilters to function or for filtering to occur via URL
where STRING null only show items that meet a certain criteria (prefix pod columns with "t." and pick columns with "column_name_here.")
groupby STRING null group records by a column (SQL: GROUP BY is used here) (advanced)
orderby STRING t.id sort the items (prefix pod columns with "t." and pick columns with "column_name_here.") (SQL: ORDER BY is used here)
page INT 1 the current page of results to show (controlled by Pod::getPagination or via URL)
limit INT 15 the number of items per page, set to -1 for no limit
sql STRING null use your own SQL SELECT statement (advanced)
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.
  1. To reference a non-PICK column, prefix the column name with "t." (e.g. "t.amount")
  2. To reference PICK column, use the column_name[dot]related_column_name (e.g. "person.first_name")

Returns

Nothing

Examples

<?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';