Blog

PodsCast #001: Find’ing Your Way with Pods:Find()

We are happy to announce the PodsCast, a live, bi-monthly video podcast with Pods team members. Each week we will spend about an hour talking about a specific part of Pods and answering user questions. Users can submit their questions on twitter using the hashtag #podscast or the #podscast channel in our Slack.

For our first episode we will be investigating the find method of the Pods class–our version of WP_Query. It’s at the heart of almost everything you do with Pods. We will be discussing how it works, how to use it and how to read the big green chart on its docs page.

You can watch live, at 2PM eastern on Tuesday March 2nd, or view the recording afterwords right here:

 

Show Notes


<?php
////////////////
// User Example
////////////////
$user = pods( 'user' ); // User
$params = array(
'orderby' => 't.user_login',
'limit' => 10,
'where' => '
(
favorite_animal.post_title = "Elephant" OR
favorite_animal_custom.meta_value = "Elephantus"
) AND
favorite_animal.related_animals.color.meta_value = "Green"
'
);
$user->find( $params );
// Loop through all users
while ( $user->fetch() ) {
echo '<p>' . $user->display( 'display_name' ) . '</p>';
}
////////////////
// CPT Example
////////////////
$event = pods( 'event' ); // CPT
// Get it from a variable
$series = 'Jim True Presents';
$params = array(
// t = wp_posts
'where' => '
t.post_title = "X" OR
t.post_name = "x" OR
t.ID = 123 OR
my_custom_field.meta_value = "Some value" OR
speaker.post_title IN ( "Jim", "Phil", "Josh", "Scott" ) OR
speaker.speaker_series.post_title = "' . pods_sanitize( $series ) . '"
' // Always sanitize variables going into find()
);
// WP_Query meta_query syntax example for 'where'
$params = array(
// t = wp_posts
'where' => array(
'relation' => 'OR',
array(
'key' => 'post_title',
'value' => 'X'
),
array(
'key' => 'post_name',
'value' => 'x'
),
array(
'key' => 'my_custom_field',
'value' => 'Some value'
),
array(
'key' => 'speaker.post_title',
'value' => array(
'Jim',
'Phil',
'Josh',
'Scott'
),
'compare' => 'IN'
// Other options for 'compare' (same as WP_Query meta_query supports)
// 'compare' => '=',
// 'compare' => '!=',
// 'compare' => '>',
// 'compare' => '>=',
// 'compare' => '<',
// 'compare' => '<=',
// 'compare' => 'LIKE',
// 'compare' => 'NOT LIKE',
// 'compare' => 'IN',
// 'compare' => 'NOT IN',
// 'compare' => 'BETWEEN',
// 'compare' => 'NOT BETWEEN',
// 'compare' => 'EXISTS',
// 'compare' => 'NOT EXISTS',
),
array(
'key' => 'speaker.speaker_series.post_title',
'value' => 'Jim True Presents'
)
)
);
$event->find( $params );
////////////////
// Other Pods
////////////////
$comment = pods( 'comment' ); // Comments
$category = pods( 'category' ); // Taxonomy
$author = pods( 'author' ); // Advanced Content Type