Using WordPress transients for storing query results

While developing a WorPress application you will eventually come across a situation where you need to use a piece of data frequently in your code. For example say, I maintain a visit count for visits by users for every post. This can be done by incrementing meta value of post ‘user_visits’ every time a post is visited. Now I want to show post from last 2 days which has visits more than 1000 on post archive page.
This can be achieved using query

$args = array(
    'post_status' => 'publish',
    'meta_query' => array(
    	'key'     => 'user_visits',
    	'value'   => 1000,
    	'compare' => '>',
    	'type'    => 'NUMERIC',
    ),
    'date_query' => array(
         array(
             'after'     => '2 days ago',  // or '-2 days'
             'inclusive' => true,
         ),
     ),
);
$posts = new WP_Query( $args );

if ( $posts->have_posts() ) {
    echo '
<ul>';
    while( $posts->have_posts() ) {
        echo '
	<li><a href="' . get_permalink() . '">' . the_title( '', '', false ) . '</a></li>
';
    }
    echo '</ul>
';
}

Looks pretty straight forward with the use of meta query and date query, right?
But keep in mind that this query is going to fire every time we load the archive post page. Definitely huge impact on the performance here.

Let’s bring transients into the picture here and modify the above code. Transients in WordPress do have expiration time so we are going to utilize that. Let’s store this query result in transient and set expiration time of 1 hour so that after every on hour it will update the result of the query.

$posts = get_transient( 'top_visited_posts' );

if ( false === $posts ) {
	$args = array(
	    'post_status' => 'publish',
	    'meta_query' => array(
	    	'key'     => 'user_visits',
	    	'value'   => 1000,
	    	'compare' => '>',
	    	'type'    => 'NUMERIC',
	    ),
	    'date_query' => array(
	         array(
	             'after'     => '2 days ago',  // or '-2 days'
	             'inclusive' => true,
	         ),
	     ),
	);
	$posts = new WP_Query( $args );

	set_transient( 'top_visited_posts', $posts, HOUR_IN_SECONDS );
}

if ( $posts->have_posts() ) {
    echo '
<ul>';
    while( $posts->have_posts() ) {
        echo '
	<li><a href="' . get_permalink() . '">' . the_title( '', '', false ) . '</a></li>
';
    }
    echo '</ul>
';
}

Now our code is ready to utilize the power of the transients.
The expensive query will be fired once every hour only.
On top of that transient utilizes the object cache if in place so that site can be benefited that way too.

Setting a WordPress site in local development environment.

Well here I am going to tell about how to setup a fresh WordPress site . I will assume that you know how to handle WAMP server and can use MySql.

All the steps in this article are for wamp server and local development environment.

All you need is working WAMP server. And a latest WordPress setup files package downloaded from wordpress.org.

First locate the directory wamp/www on drive where you have installed wamp server. Extract the zip downloaded from WordPress site to this directory. Now you have WordPress directory ready to set up as new site.
Rename this directory to your sites name.

Next step, create a database in MySql (Give it a proper name).

Now its time to install site. Open your favorite browser and hit url localhost/your-site-name

It will bring up screen where installation precess will guide you through simple instructions. Fill up information like:

  1. Database name you recently created
  2.  Database user name
  3. Password
  4. Database host name ( localhost)

In further steps you need to fill in details:

  1. Site name
  2. Admin user name
  3. Password
  4. Email for admin purpose.

When you have filled in all data, installation script will setup WordPress site for you.

Or you can setup by putting these details in wp-config file.

Some things to remember:

wp-config file for local site setup is not useful for server setup when you go live with your local setup.
Same story for .htaccess file; don’t upload a local .htaccess file on server.
If you use any version control tool, don’t push wp-config and htaccess files to repository. Best way add them to gitignore list.