How to use wp_query(), query_posts() and pre_get_posts functions Properly |

Congrats, You are Subscribed to Receive Updates.

How to use wp_query(), query_posts() and pre_get_posts functions Properly

How to use wp_query(), query_posts() and pre_get_posts() functions Properly. Generally, the WordPress developers may have some confusions, while selecting suitable functions for the right operations and results. These three functions looking one and the same. But each one have some distinct features to talk. Here let me give you simple explanations for the better use.


Let’s start with wp_query() function.

1.wp_query() : 

This is the main query of the instance object. Which helps to decide the right template for the current use. For example, you can pass the page navigation and url . This will mainly creates the posts list from the database. Also you can use it based on your customization’s. Just like the following example,

$kv_custom_posts_list = new WP_Query( //arguments );
if( $kv_custom_posts_list->have_posts() ):
    while( $kv_custom_posts_list->have_posts() ): $kv_custom_posts_list->the_post();
       //The custom posts loop

the custom posts loops may be used on the custom posts list, say for example, popular post, category post, and widget posts which is most useful on side bars. And you have to notice an important function here, ” wp_reset_postdata() ” . This helps to stop overriding the main posts loop, that’s is global and main one.  The global $post variable, holds the main posts loop. Which will not get affected, with the help of wp_reset_postdata(). The main arguments are available at WordPress Codex Page.  Click Here .

2. Query_posts() : 

This function is also connected with wp_query for the posts list. Also this gets the simliar kind of results, but all not in loop. Query posts help you to perform simple operations like, sorting, filtering and adding more query classes to narrow down the results. The returned result is also the Post, but it shouldnt affect any other global variables and functions.

query_posts( 'cat=23, 7, 53, 42' );  // filter the category id's

query_posts( 'p=27' );  // resurns a single post.

query_posts( 'attachment_id=12' ); // returns the attachment else returns null. 

 3. pre_get_posts:

This is actually a WordPress filter not function. It will help you to hook the custom functions on the querying posts.  The following one  describes you to know more about the pre_get_posts.

add_action( 'pre_get_posts', 'kv_kvcodes_custom' );

function kv_kvcodes_custom( $query ) {
    if ( $query->is_home() && $query->is_main_query() ) {
        $query->set( 'cat', '23' );

While returning results, the posts list will be short listed to the category id which we passed on to the set function. Also, there is another function is available to get the posts list. get_posts() –  This will also helps you to get the posts list. Take a look at the following code for more detailed informations.

global $post;
$args = array( 'numberposts' => 10, 'offset'=> 1, 'category' => 8 );
$myposts = get_posts( $args );
foreach( $myposts as $post ) :  setup_postdata($post); 
endforeach; wp_reset_postdata();

The above code returns 10 posts on category 8.


About Varadharaj V

The founder of Kvcodes, Varadharaj V is an ERP Analyst and a Web developer specializing in WordPress(WP), WP Theme development, WP Plugin development, Frontaccounting(FA), Sales, Purchases, Inventory, Ledgers, Payroll & HRM, CRM, FA Core Customization, PHP and Data Analyst. Database Management Advance Level

Comment Below

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


Current ye@r *