A Look Into: 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 new WP_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.

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> <?php endwhile; else: ?> <p><?php _e('Sorry, no posts matched your criteria.'); ?></p> <?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.

 $my_query = new WP_Query(); 

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

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

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

 <?php if ( $my_custom_query->have_posts() ) : while ( $my_custom_query->have_posts() ) : $my_custom_query->the_post(); ?> <div class="title"> <a href="<?php the_permalink() ;?>"><?php the_title() ;?></a> </div> <?php endwhile; else: ?> <p> <?php _e('Sorry, no posts matched your criteria.'); ?> </p> <?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 pagination simply 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:

 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.

 if ( ! ( $my_query = get_transient( 'my_query_cache' ) ) ) { $my_query = new WP_Query('cat=-1,-5'); set_transient( 'my_query_cache', $my_query, DAY_IN_SECONDS ); } 

Wrap Up

With WP_Query, we can create a simple or more complex queries. If writing a custom WP_Query seems 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:


    



via hongkiat.com http://rss.feedsportal.com/c/35261/f/656072/s/356987c5/sc/4/l/0L0Shongkiat0N0Cblog0Cwordpress0Equery0Ebasic0C/story01.htm