Yogesh Chauhan's Blog

WP_Query Class in WordPress

in WordPress on February 9, 2021

WP_Query Class is as the name suggests The WordPress Query class.

Mostly we apply direct functions in WordPress files if we are developing a theme. So for a basic theme we hardly deal with advanced level queries and the class internals as well as the global variables.

WordPress has many inbuilt different functions using which we can get the information we need in different files.

Still, there are few scenarios in which we would want to use WP_Query.

The first scenario is to find out what type of request WordPress is currently dealing with.

The $is_* properties are designed to hold this information. We can use the Conditional Tags to interact with that information. In many cases, you would want to interact with this when you’re writing your plugin or a theme.

The second scenario is during The Loop.

WP_Query provides number of functions for many common tasks within The Loop.

For example, when we write have_posts(), it will call $wp_query->have_posts() function and check if there are any posts to show.

If there are posts then we can start a while loop using have_posts() function as the condition. This will iterate around as long as there are posts to show.

In each iteration we can call the_post(), which is going nothing but $wp_query->the_post() function. Calling that function will set up internal variables within $wp_query and the global $post variable.

These are the functions you should use when writing a theme file that needs a loop.

If you use the_post() with your query, you need to run wp_reset_postdata() afterwards to have template tags use the main query’s current post again.

πŸ›πŸ›πŸ› Also, for querying posts in the admin, consider using get_posts() as wp_reset_postdata() might not behave as expected. Here is more info on the bug.

How to use WP_Query?

We can just use a standard loop.

// The Query
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
    echo '<ul>';
    while ( $the_query->have_posts() ) {
        echo '<li>' . get_the_title() . '</li>';
    echo '</ul>';
} else {
    // no posts found
/* Restore original Post Data */

We can use pagination in a standard loop.

// the query
$the_query = new WP_Query( $args ); 

if ( $the_query->have_posts() ) : 
// pagination

   while ( $the_query->have_posts() ) : $the_query->the_post(); 
     <h2><?php the_title(); ?></h2>

<?php endwhile; 

<?php else : ?>
    <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>

We can use multiple loops.

// The Query
$query1 = new WP_Query( $args );
// The Loop
while ( $query1->have_posts() ) {
    echo '<li>' . get_the_title() . '</li>';

/* The 2nd Query (without global var) */
$query2 = new WP_Query( $args2 );
// The 2nd Loop
while ( $query2->have_posts() ) {
    echo '<li>' . get_the_title( $query2->post->ID ) . '</li>';
// Restore original Post Data

Credit: WordPress

Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #3 How to add Read More Read Less Button using JavaScript? #4 How to uninstall Cocoapods from the Mac OS? #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to Use SQL MAX() Function with Dates?

Recently Posted

#Aug 15 Is PHP still good for back-end programming? #Aug 10 How to create a multisite network in WordPress? #Aug 3 How to create a circle that follows a cursor using JavaScript and CSS? #Aug 3 How to make a curtain slider using jQuery and CSS? #Aug 2 How to progressively load images and add a blurry placeholder? #Aug 1 How to create a placeholder loader (throbber) using CSS?
You might also like these
The basics of CSS Box modelCSSHow to import a CSS file using PHP code and not HTML code?PHPHow to render lists inside a component in React?ReactKubernetes vs. Docker? It’s a misleading phraseMiscellaneousHow to make WordPress main stylesheet (style.css)?WordPressVariables and Distance Functions in SCSS (Sass)SCSS