Created
February 10, 2016 17:07
-
-
Save bmodena/12ce50a9f4f3b4075286 to your computer and use it in GitHub Desktop.
wordpress 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
<?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