Include and Init the Pods class
Contents
Function Definition
function pods ( $type = null, $id = null, $strict = false )
Source File: /pods/includes/classes.php
Parameters
PARAMETER | TYPE | DETAILS |
---|---|---|
$type | (string) | The pod name |
$id | (mixed) | (optional) The ID or slug, to load a single record; Provide array of $params to run ‘find’ |
$strict | (bool) | (optional) If set to true, returns false instead of a Pods object, if the Pod itself doesn’t exist. Note: If you want to check if the Pods Item itself doesn’t exist, use pods::exists(). |
Returns
(bool|Pods)
Examples
<?php // Get the Pods object $mypod = pods( 'mypod' ); // Get the Pods object for a specific item $mypod = pods( 'mypod', $id_or_slug ); // Get the Pods object and run find() $params = array( 'orderby' => 't.name DESC', 'limit' => 15, 'where' => 't.name != "Buster"' ); $mypod = pods( 'mypod', $params ); // The above is the same as $mypod = pods( 'mypod' )->find( $params ); // And the above is the same as $mypod = pods( 'mypod' ); $mypod->find( $params ); // Loop through the items returned while ( $mypod->fetch() ) { echo '<p>' . $mypod->display( 'my_field_name' ) . '</p>'; } // Check to see if an item was found or not // at http://mysite.com/bunnies/voodoo-rabbit/ $slug = pods_var( 1, 'url' ); // get the second level of the current URL $pods = pods( 'bunny', $slug ); if ( $pods->exists() ) { // This bunny exists! } else { // This bunny was not found, better send a 404. // This only works if it's ran in a Pod Page Precode // or if you set the $pods global to this before the // theme runs, which is usually right after the // 'after_setup_theme' action. $pods = 404; }
Add Default Values To Fields
Output a Pods Form for Pod ‘fruit’ with two fields, ‘yellow_fruit’ and ‘blue_fruit’ with the field ‘yellow_fruit’ pre-populated with the value ‘Banana’.
<?php // Create the pods object. $pods = pods( 'fruits' ); // Setup the fields list. $params = array( 'yellow_fruit' => array( 'default' => 'Banana' ), 'orange_fruit' ); // Output the form. echo $pods->form( $params ); ?>
Use strict mode and pods::exists() to avoid errors if Pod or Pod item do not exist
Illustrates the difference between strict mode, which returns false if the pod does not exist, versus pods::exists() (does the item exist).
<?php $pods = pods( 'fruits', 'satsuma', true ); // Check if the pod is valid. if ( false !== $pods ) { // Check if the pod item exists. if ( $pods->exists() ) { // The pod item exists. } }