Creating Custom WordPress Query

Today, we are going to take a look at WordPress Query. Even though WordPress has documented its Query function, it is very intimidating and probably impractical to dive into each of the sections. This is the shortcut you need.

In this post we will be discussing a few practical tips on applying WordPress Query that you might need to frequently use on your theme.

Basic WP_Query

In short, WP_Query is a class to request WordPress posts and pages. We can create a newWP_Query class in a WordPress theme to query posts (or pages) in customized specification.

First, let’s take a look at the index.php within your theme directory; you should find the following piece of codes.

  1. <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
  2. <?php endwhile; else: ?>
  3. <p><?php _e(‘Sorry, no posts matched your criteria.’); ?></p>
  4. <?php endif; ?>

It’s called Loop. It by default displays all the posts that have been published.

Let’s see how we can customize it with WP_Query. In this example, we are going to exclude posts from a specific category.

First, we set a new WP_Query in a variable.

  1. $my_query = new WP_Query();

Within it, we can assign the category ID that we want to exclude. For example:

  1. $my_query = new WP_Query(‘cat=-1,-5’); //exclude category 1 and 5

Then, we refer to the variable within the Loop, as follow.

  1. <?php if ( $my_custom_query->have_posts() ) : while ( $my_custom_query->have_posts() ) : $my_custom_query->the_post(); ?>
  2.     <div class=”title”>
  3.         <a href=”<?php the_permalink() ;?>”><?php the_title() ;?></a>
  4.     </div>
  5. <?php endwhile; else: ?>
  6.     <p>
  7.         <?php _e(‘Sorry, no posts matched your criteria.’); ?>
  8.     </p>
  9. <?php endif; ?>

Note that when you have multiple custom queries, particularly within a single page, you have to enclose it with wp_reset_postdata().

Assigning Query With WP-PageNavi

WP-PageNavi is probably the most popular plugin to add numbered pagination in WordPress. Most people however encounter an error when they use it along with custom WP_Query. The paginationsimply won’t work.

Since version 2.74, WP-PageNavi has provided an option where you can assign custom query. Taking the above custom query as our example, we can do the following:

  1. wp_pagenavi( array( ‘query’ => $my_query ) );

… and that should solve the error.

Cache Query

It should be noted that when you have multiple queries, particularly on a single page, you will end up with multiple server loads which could affect your site load performance.

One of the ways to optimize it is by using Transient API. In this case, we use it to cache the queried object from WP_Query, and store it in a certain period of time.

That way, we can speed up query load times by retrieving and processing the cached data rather than querying it everytime the page is loaded.

Here is a code example, where the cache is stored for 24 hours.

  1. if ( ! ( $my_query = get_transient( ‘my_query_cache’ ) ) ) {
  2.     $my_query = new WP_Query(‘cat=-1,-5’);
  3.     set_transient( ‘my_query_cache’, $my_query, DAY_IN_SECONDS );
  4. }

Wrap Up

With WP_Query, we can create simple or more complex queries. If writing a custom WP_Queryseems complicated to you, there is a tool called WP_Query Generator that will make it easier.

I hope you will find this tip useful, and if you want to dig into this topic further, below are some references:

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s