Skip to content

Instantly share code, notes, and snippets.

@bmodena
Created February 10, 2016 17:07
Show Gist options
  • Save bmodena/12ce50a9f4f3b4075286 to your computer and use it in GitHub Desktop.
Save bmodena/12ce50a9f4f3b4075286 to your computer and use it in GitHub Desktop.
wordpress transient
<?php
/* Credit Scotch.io : https://scotch.io/tutorials/a-guide-to-transients-in-wordpress */
// Set variable for debugging
$usecache = true;
// Check if transient exists - use if it does
$projects = get_transient( 'tmbr_cached_projects' );
if ( false === $projects || false === $usecache ) {
$args = array(
'post_type' => 'project',
'post_status' => 'publish'
);
$projects = new WP_Query( $args );
// set cache for 1 day
set_transient( 'tmbr_cached_projects', $projects, DAY_IN_SECONDS );
}
if( $projects->have_posts() ) {
echo '<ul>';
while( $projects->have_posts() ) {
echo '<li><a href="' . get_permalink() . '">' . the_title( '', '', false ) . '</a></li>';
}
echo '</ul>';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment