public function find ( $params = null, $limit = 15, $where = null, $sql = null )
Find items of a pod, much like WP_Query, but with advanced table handling.
We covered this method extensively in the PodsCast episode #1
Source File: /pods/classes/Pods.php
Contents
Parameters
PARAMETER | TYPE | DETAILS |
---|---|---|
$params | (array|string|int) | An associative array of parameters, or the ID of the item, or for advanced content types the item’s slug. |
$limit | (int) | (optional) (deprecated) Limit the number of items to find, use -1 to return all items with no limit |
$where | (string) | (optional) (deprecated) SQL WHERE declaration to use |
$sql | (string) | (optional) (deprecated) For advanced use, a custom SQL query to run |
Additional Parameter Options
Returns
(Pods) The pod object
Examples
Example 1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
<?php // Find can be called in one of three ways // Example #1 $mypod = pods( 'mypod', $params ); // Example #2 $mypod = pods( 'mypod' )->find( $params ); // Example #3 $mypod = pods( 'mypod' ); $mypod->find( $params ); // Here's how to use find() $params = array( 'limit' => 3, 'page' => 2, // Be sure to sanitize ANY strings going here 'where'=>"category.name = 'My Category'" ); // Run the find $mypod = pods( 'mypod', $params ); // Loop through the records returned while ( $mypod->fetch() ) { echo $mypod->display( 'name' ) . "\n"; } |
The above example will output:
1 2 3 |
Pod Title 1 Pod Title 2 Pod Title 3 |
Using a variable in a where clause
When the where clause must be created dynamically, a variable can be passed to it like in this example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<?php // get value from input to a form $keyword = like_escape( pods_v_sanitized( 'keyword', 'post' ) ); // set up find parameters, where meta field title matches $keyword $params = array( 'where' => 't.post_title LIKE "%' . $keyword . '%" OR my_field.meta_value LIKE "%' . $keyword . '%"' ); //search in articles pod $pods = pods( 'articles', $params ); //loop through results if ( 0 < $pods->total() ) { while ( $pods->fetch() ) { echo $pods->display( 'issue' ); } } ?> |
See Also
Find Reference Table
Reference table for use in where clauses in Find and Pods Shortcodes.