Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Speed Up Your WordPress Website by Caching Custom Queries using Transients API

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 795
    Comment on it

    Hello readers!

    If you want to make your WordPress website faster you can make it possible by Caching Custom Queries using Transients API. So read out this tutorial carefully and apply the steps in your website.

    If you are using custom WordPress queries to show random posts, recent posts, popular posts or etc in the sidebar or anywhere, then you should consider using the WordPress transient API to cache these queries to reduce resource expenditure as well as helping the load time.

    Now I will let you know about,


    How to make Your WordPress website faster by caching custom queries using the Transients API.

    First of all you need to understand that how your WordPress themes work (loops, wp_query etc), in order for you to follow this post.

    Let me explain what it does?

    Basically if you are running a WordPress site like wpdemo32 and it have a loop that shows 5 random posts in sidebar, then transient API can help. Every time whenever a user refresh the page, that custom WP Query that you have to get 5 random post will go in your database and randomly it will pull 5 posts.
    If you are getting A LOT of people to your site in other words High Traffic, then it can crash your SQL server, and you will see the Error Establishing Database Connection screen.

    Now by adding a few extra lines of code which is mentioned below, you can easily store the results of that query (in cache) for a particular period of time by using the Transients API.

    Example of the code that we generally use in our WordPress site for getting 5 random posts-

    <?php
    $args = array(
     'orderby'    => 'rand',
     'posts_per_page' => 6'
    );
    $query = new WP_Query( $args );
    if( $query->have_posts() )
    {
     while( $query->have_posts() )
     {
      $query->the_post();
      ?>
      <div class="container_grid">
       <div class="thumb_grid"><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_post_thumbnail(); ?></a></div>
       <div class="content_grid">
        <div class="text_grid"><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></div>
       </div>
      </div>
      <?php
     }
    }
    else
    {
     echo "No posts found !!!";
    } ?>
    

    The above code showed new content every time. Now by caching the query for 12 hours, we will get the same 5 posts showing for 12 hours.
    One important note- I have not used the WP_Query for Caching Custom Queries using Transients API. I have used only get_posts query for getting 20 posts. I would like you to please use these tutorial for "WP_Query" as like as "get_posts" and let me know your feedback.

    Now please use below mentioned code to make cache using Transients API

    <?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
     $rand_args = array(
      'orderby'   => 'rand',
      'posts_per_page'  => 20
     );
     $special_query_results = new get_posts( $rand_args );
        set_transient( 'special_query_results', $special_query_results, 60*60*12 );
     //set_transient( $transient, $value, $expiration );
    } ?>
    

    Note- The 606012 is that area where you can control the length of the cache. Now if we show the $special_query_results using the for each loop, we will have all 20 posts displayed. So we need to utilize the array_rand() function to only pull 5 post items at random.
    We added the code like this:

    <?php
    $random_posts = get_transient( 'special_query_results' );
    $rand_key = array_rand( $random_posts, 5 );
    ?>
    

    Now this will get 5 post ID's at random from our transient data.
    However, it will not get the values for each post. So we had to do add this bits of code:

    <?php
    $transientposts[0] = $random_posts[$rand_key[0]];
    $transientposts[1] = $random_posts[$rand_key[1]];
    $transientposts[2] = $random_posts[$rand_key[2]];
    $transientposts[3] = $random_posts[$rand_key[3]];
    $transientposts[4] = $random_posts[$rand_key[4]];
    ?>
    

    After this, we are ready to display the loop. Now simply put the code like this:

    <?php
    global $post;
    foreach( $transientposts as $post )
    {
     setup_postdata($post);
     //All the items go here.
    } ?>
    

    Important Note*- Setup_postdata allows you to use all loop tags inside this for each loop such as the_permalink etc.

    To easy for everybody, here is the final code for Caching Custom Queries using Transients API that we have:

    <?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
     $rand_args = array(
      'orderby'   => 'rand',
      'posts_per_page'  => 20
     );
     $special_query_results = new get_posts( $rand_args );
        set_transient( 'special_query_results', $special_query_results, 60*60*12 );
     //set_transient( $transient, $value, $expiration );
    }
    
    // Use the data like you would have normally...
    $random_posts = get_transient( 'special_query_results' );
    $rand_key = array_rand( $random_posts, 5 );
    $transientposts[0] = $random_posts[$rand_key[0]];
    $transientposts[1] = $random_posts[$rand_key[1]];
    $transientposts[2] = $random_posts[$rand_key[2]];
    $transientposts[3] = $random_posts[$rand_key[3]];
    $transientposts[4] = $random_posts[$rand_key[4]];
    
    global $post;
    
    foreach( $transientposts as $post )
    {
     setup_postdata($post);
     ?>
     <div class="container_grid">
      <div class="thumb_grid"><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_post_thumbnail(); ?></a></div>
      <div class="content_grid">
       <div class="text_grid"><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></div>
      </div>
     </div>
     <?php
    } ?>
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: