Wednesday, June 6, 2012

Speed up your blog by caching custom queries

Is your theme using custom queries? If yes, you should definitely use WordPress Transients API to cache the queries and consequently speed up your blog. Today’s recipe will show you how to cache any custom queries.


<?php  // Get any existing copy of our transient data  
if ( false === ( $special_query_results = get_transient( 'special_query_results' ) ) )
 {     
 // It wasn't there, so regenerate the data and save the transient    
   $special_query_results = new WP_Query( 'cat=5&order=random&tag=tech&post_meta_key=thumbnail' ); 
   set_transient( 'special_query_results', $special_query_results ); 
 } 
   // Use the data like you would have normally... 
 ?>


No comments:

Post a Comment