Home > Article > Backend Development > Merge WP_Query with main query
So far in this series, you have learned how to use WP_Query
to create a custom query for use in a theme or plugin.
In most cases you will be using WP_Query
with a whole new set of parameters that are separate from the parameters in the main query, but what if you want to include the main query in the parameters?
Examples of where you might want to do this include:
I could go on and on, there are many opportunities to combine the main query with your own custom query.
I will demonstrate this with three examples: the first is a simple example with a loop; the second will use foreach
to output multiple loops, one for each three post types; the third will output both post types on the category archive using two separate queries.
However, you are combining the main query with the WP_Query
, you need to store the current query object arguments in a way that is easy to use in the WP_Query
argument. The easiest way is to assign it to a variable.
Do this before defining the WP_Query
parameter like this:
$mainquery = get_queried_object();
get_queried_object()
The function returns the currently queried object, no matter what the object is. On a single post it will only return the post object, while on an archive it will return a category, tag, term object or any object related to the archive. It returns the ID of the query object.
You can then use this $mainquery
variable in the WP_Query
parameter. Now let's look at some examples.
Suppose you have a custom post type added to your website and you have enabled categories for that custom post type. On the category archive for each category, you don't want to show posts: instead, you want to show posts of a new post type - let's call it product
.
Your query might look like this:
<?php $mainquery = get_queried_object(); $args = array ( 'category_name' => $mainquery->slug, 'post_type' => 'product' ); // Custom query. $query = new WP_Query( $args ); // Check that we have query results. if ( $query->have_posts() ) { // Start looping over the query results. while ( $query->have_posts() ) { $query->the_post(); // Contents of the queried post results go here. } } // Restore original post data. wp_reset_postdata(); ?>
Since the category_name
parameter I used above takes the category slug as a parameter, I need to add ->slug
after the variable to output the category slug.
This gives you a query that fetches posts of product
post type from the database with the current query category. You can use it on the category.php
page template.
Note: You can also achieve this result by modifying the main query using the pre_get_posts
hook and combining it with a conditional function to check the category archives.
The next example will output all posts from the current category page, but instead of displaying them all in one block, they will be separated by post type.
This means you can use CSS to categorize post types into blocks or columns on the page, or just separate them into different lists.
To do this you can use the following code:
<?php $mainquery = get_queried_object(); $post_types = get_post_types(); foreach ( $post_types as $post_type ) { $args = array( 'category_name' => $mainquery->slug, 'post_type' => $post_type ); // Custom query. $query = new WP_Query( $args ); // Check that we have query results. if ( $query->have_posts() ) { // Start looping over the query results. while ( $query->have_posts() ) { $query->the_post(); // Contents of the queried post results go here. } } // Restore original post data. wp_reset_postdata(); } ?>
This uses the $mainquery
variable we used before, but it also adds a $post_types
variable to store all the post types registered on the site, as well as a $post_type
Variables store each individual post type in turn.
The last example is similar to the second example, but splits the post type into two separate queries, each with its own different loop. This gives you more control over what appears on each piece of content, so you can display your posts differently than your products, perhaps including featured images of your products or giving them a different layout.
Suppose your website registers the product
post type and has categories enabled for it, and you are also writing a blog post with the same categories. On each category archive page, you want to display the ten most recent posts, and then you want to display a list of all products in the same category.
To do this, you can use code similar to:
<?php $mainquery = get_queried_object(); // First query arguments for posts. $args = array ( 'category_name' => $mainquery->slug, 'post_type' => 'post', 'posts_per_page' => '10' ); // Custom query. $query = new WP_Query( $args ); // Check that we have query results. if ( $query->have_posts() ) { // Start looping over the query results. while ( $query->have_posts() ) { $query->the_post(); // Contents of the queried post results go here. } } // Restore original post data. wp_reset_postdata(); // Second query arguments for products. $args = array ( 'category_name' => $mainquery->slug, 'post_type' => 'product', 'posts_per_page' => '-1' ); // Custom query. $query = new WP_Query( $args ); // Check that we have query results. if ( $query->have_posts() ) { // Start looping over the query results. while ( $query->have_posts() ) { $query->the_post(); // Contents of the queried post results go here. } } // Restore original post data. wp_reset_postdata(); ?>
You can then write each loop differently to output different data for each post type.
As you can see from the example above, using WP_Query
not only allows you to create a completely custom query separate from the main query, but you can also merge the objects of the current query and create a more powerful query on the archive page .
The above example can also be done using other archive types: for category, author, date, etc. See if you can think of more possibilities!
The above is the detailed content of Merge WP_Query with main query. For more information, please follow other related articles on the PHP Chinese website!