Last active
December 16, 2015 11:58
-
-
Save topher1kenobe/5430835 to your computer and use it in GitHub Desktop.
Store WordPress query in a transient
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Let's create the function to be used as a shortcode | |
function featured_coaches($atts) { | |
// My content type is for Motor Coaches, and we want to get the ones that have | |
// the Featured box checked, but not the Sold box. | |
// I'm starting out saying we want 3, and no sold | |
extract(shortcode_atts(array( | |
'num' => 3 | |
,'sold' => false | |
), $atts)); | |
// instantiate my output variable | |
$output = ''; | |
// create the transient name | |
$transient_name = 'featured_coaches'; | |
// try getting the transient. | |
$featured_coaches = get_transient( $transient_name ); | |
// if the get works properly, I should have an object in $featured_coaches. | |
// If not, run the query. | |
if(!is_object($featured_coaches)) { | |
// My actual query is kind of irrelevant to this tutorial, but I'm getting | |
// published coaches, no_found_rows is on, ordering by date, and getting the | |
// number indicated in the $num var | |
$args = array( | |
'post_status' => 'publish' | |
,'post_type' => 'coaches' | |
,'orderby' => 'date' | |
,'posts_per_page' => $num | |
,'no_found_rows' => true | |
); | |
// here's where I'm setting up the Sold option | |
if($sold == false) { | |
$args['meta_key'] = 'ecpt_sold'; | |
$args['meta_value'] = 'on'; | |
$args['meta_compare'] = 'NOT EXISTS'; | |
} else { | |
$args['meta_key'] = 'ecpt_sold'; | |
$args['meta_value'] = 'on'; | |
} | |
// here's where I'm taking care of the featured option | |
$args['meta_key'] = 'ecpt_featured'; | |
$args['meta_value'] = 'on'; | |
$args['meta_compare'] = 'EXISTS'; | |
// Run the query | |
$the_query = new WP_Query( $args ); | |
// save the results of the query with a 12 hour timeout | |
$save_query = set_transient($transient_name,$the_query,60*60*12); | |
// load up $featured coaches with the results of the query. Don't get the | |
// transient because it would be the same as the contents of $the_query at | |
// this point, and we don't want another database call | |
$featured_coaches = $the_query; | |
// end object test | |
} | |
// from here on it's a normal WP_Query setup, but we're looking inside | |
// #featured_coaches for our stuff | |
// The Loop | |
while ( $featured_coaches->have_posts() ) : $featured_coaches->the_post(); | |
$output .= get_template_part('loop', 'coach'); | |
endwhile; | |
/* Restore original Post Data | |
* NB: Because we are using new WP_Query we aren't stomping on the | |
* original $wp_query and it does not need to be reset. | |
*/ | |
wp_reset_postdata(); | |
return $output; | |
// end recent_coaches | |
} | |
add_shortcode('featured_coaches','featured_coaches'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment