Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
WordPress Transients API: Cache complex queries, especially those that are used in several locations
<?php
function create_pulldown_transient() {
//movies picker
$output = '';
$output = '<!-- transient created ' . date( 'r' ) . ' -->';
$output .= '<select id="mt-movies-select" class="mt-movies-jump">';
$output .= '<option value="">Pick a movie ...</option>';
$movies = get_posts(
array(
'posts_per_page' => -1,
'post_type' => 'mt_movie',
'meta_key' => 'movie_nowplaying',
'meta_value' => '1',
'orderby' => 'title',
'order' => 'ASC'
)
);
foreach( $movies as $movie ) {
$output .= '<option value="' . get_post_permalink( $movie->ID ) . '">' . $movie->post_title . '</option>' . "\n";
}
$output .= '</select>';
$output .= '<select id="mt-theaters-select" class="mt-movies-jump">';
$output .= '<option value="">or Pick a theater ...</option>';
$theaters = get_posts(
array(
'posts_per_page' => -1,
'post_type' => 'mt_theater',
'orderby' => 'title',
'order' => 'ASC'
)
);
foreach( $theaters as $theater ) {
$output .= '<option value="' . get_post_permalink( $theater->ID ) . '">' . $theater->post_title . '</option>' . "\n";
}
$output .= '</select>';
return $output;
}
function create_movies_transient() {
$output = '';
//get timestamps for start and end of this week
date_default_timezone_set( 'GMT' );
$start = new DateTime( 'Sunday last week midnight' );
$end = new DateTime( 'Saturday this week 23:59:59' );
//get all nowplaying movies new this week
$query = new WP_Query(
array(
'post_type' => 'mt_movie',
'meta_query' => array(
array(
'key' => 'movie_releasedate',
'value' => array( $start->format( 'U' ), $end->format( 'U' ) ),
'compare' => 'BETWEEN',
'type' => 'NUMERIC'
)
),
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'ASC'
)
);
$output = '<!-- transient created ' . date( 'r' ) . ' -->';
$output .= '<h2>NEW THIS WEEK</h2>';
$has_new_movies = false;
if( $query->have_posts() ) {
$new_movies_segment = '<ul>';
while( $query->have_posts() ) {
$query->next_post();
$msdate = get_post_meta( $query->post->ID, 'movie_releasedate', true );
$today = mktime( 23, 59, 59, date( 'n' ), date( 'j' ), date( 'Y' ) );
//make sure "new this week" movie isn't a one-day engagement prior to today
if( ( '1' != get_post_meta( $query->post->ID, 'movie_nowplaying', true ) && $msdate > $today ) || '1' == get_post_meta( $query->post->ID, 'movie_nowplaying', true ) ) {
$has_new_movies = true;
$new_movies_segment .= '<li>';
$new_movies_segment .= '<a href="' . get_post_permalink($query->post->ID) . '">';
if( ! empty( get_the_post_thumbnail($query->post->ID, 'full') ) ) {
$new_movies_segment .= get_the_post_thumbnail( $query->post->ID, 'full' );
}
else {
$new_movies_segment .= '<img src="/wp-content/uploads/2014/04/nomovie.png" />';
}
$new_movies_segment .= '</a>';
if( $msdate > $today && '1' != get_post_meta( $query->post->ID, 'movie_nowplaying', true ) ) {
$new_movies_segment .= '<span class="mt-movie-opendate">Opens ' . date( 'l', $msdate ) . '</span>';
}
$new_movies_segment .= '<h3><a href="' . get_post_permalink($query->post->ID) . '">' . $query->post->post_title . '</a></h3>';
$new_movies_segment .= '<p>' . $query->post->post_excerpt . '</p>';
$new_movies_segment .= '</li>';
}
}
$new_movies_segment .= '</ul>';
}
if( !$has_new_movies ) {
$new_movies_segment = '<p><em>No new movies this week.</em></p>';
}
$output .= $new_movies_segment;
$output .= '<h2>NOW PLAYING:</h2>';
//all nowplaying movies not new this week
$query = new WP_Query(
array(
'post_type' => 'mt_movie',
'meta_query' => array(
array(
'key' => 'movie_releasedate',
'value' => array( $start->format( 'U' ), $end->format( 'U' ) ),
'compare' => 'NOT BETWEEN',
'type' => 'NUMERIC'
),
array(
'key' => 'movie_nowplaying',
'value' => '1'
)
),
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'ASC'
)
);
if( $query->have_posts() ) {
$output .= '<ul>';
while( $query->have_posts() ) {
$query->next_post();
$output .= '<li>';
$output .= '<a href="' . get_post_permalink($query->post->ID) . '">';
if( ! empty( get_the_post_thumbnail( $query->post->ID, 'full' ) ) ) {
$output .= get_the_post_thumbnail( $query->post->ID, 'full' );
}
else {
$output .= '<img src="/wp-content/uploads/2014/04/nomovie.png" />';
}
$output .= '</a>';
$output .= '<h3><a href="' . get_post_permalink( $query->post->ID ) . '">' . $query->post->post_title . '</a></h3>';
$output .= '<p>' . $query->post->post_excerpt . '</p>';
$output .= '</li>';
}
$output .= '</ul>';
}
else {
$output .= '<p><em>No movies now playing.</em></p>';
}
return output;
}
//render page
get_header();
get_sidebar();
?>
<div class="mt-main-col">
<div class="mt-movies-picker">
<h2>Movies &amp; Showtimes</h2>
<?php
//nav pulldown menus
if( false === ( $pulldown = get_transient( 'movies_picker' ) ) {
$pulldown = create_pulldown_transient();
set_transient( 'movies_picker', $pulldown, 14 * HOUR_IN_SECONDS );
}
echo $pulldown;
?>
</div><!-- //mt-movies-picker -->
<div class="mt-movies-landing">
<?php
//movies listing
if( false === ( $movies = get_transient( 'movies_listing' ) ) {
$movies = create_movies_transient();
set_transient( 'movies_listing', $movies, 14 * HOUR_IN_SECONDS );
}
echo $movies;
?>
</div><!-- //mt-movies-landing -->
</div><!-- //mt-main-col -->
<?php
get_footer();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.