WordPress

Cache WordPress Query Strings Using Transient API

transients API In WordPress you can easily create a Query to search all the posts on your blog. All you have to do is use the WordPress WP Query Class and pass in the parameters for the custom query.

The problem with custom queries is that they will normally be returning data is isn't queried by WordPress, this means that it can take longer to return the results. With the results taking longer to come back from the database it can slow down your page loading times. Luckily WordPress comes with a Transient API where you can cache anything including results from queries.

Below is a WordPress snippet to use next time you need to make a custom query to the WordPress database.

<?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...
?>

Source: Transient API

Back to top ^

Join Paulund

  • Get access to 100s of premium tutorials and downloadable content
  • Members content consists of Premium WordPress plugins, CSS packages, jQuery packages, tutorial demo files and templates for 100s of web development tutorials
  • In-depth development tutorials
  • Priority tutorial requests
  • Priority email development support
  • No ads

Here at Paulund you'll find Web Development tutorials and snippets focusing mainly on PHP, WordPress and CSS. Members will get access to premium tutorials that go into more detail about development with these languages. You will also get access to downloadable premium content based on the tutorial.

Join Paulund

Leave a Reply

Your email address will not be published. Required fields are marked *