YogeshChauhan . com

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.



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


We can use pagination in a standard loop.



<?php 
// 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; 

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


We can use multiple loops.



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

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


Credit: WordPress

amazon

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 How to Use SQL MAX() Function with Dates? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

Jun 16 What are Stored Procedures for SQL Server? Jun 16 What are Class Constants in PHP? Jun 15 A short basic guide on states in React Jun 15 How to define constants in PHP? Jun 15 How to define visibility for a property in PHP? Jun 15 How to use @if and @else in SCSS?

You might also like these

Some Useful Operators in The SQL WHERE ClauseSQL/MySQLHow to Clone Objects in PHP?MiscHow to remove a specific item from an array in JavaScript?JavaScriptWordPress: How to find all posts from a custom post type with specific custom field?WordPressHow to embed YouTube or other video links in WordPress?WordPressClasses in JavaScript: The BasicsJavaScript