How to combine two WP_Query objects?
I need to combine two different WordPress WP_Query Objects with separate $posts_per_page
lets say we have two different categories and each of them is responsible to show 18 posts or products of their own category, something like:
$category_query = new WP_Query([
'posts_per_page' => '18',
'post_type' => 'product',
'tax_query' => [
[
'taxonomy' => 'product_cat',
'terms' => 120,
'field' => 'id',
'include_children' => false
]
]
]);
Imagine Another query just like above but with a different term_id
, as you can see posts_per_page
is 18 and for each query 18 products with specific category will be returned.
Although it is correct, but I am trying to combine the two queries to reduce number of database queries and return the total of 36 products which the first 18 includes only category number 1 and 18 others for category number 2, even if they are in random order a simple traverse through array could fix this issue.
Do you have any idea how to do that?
Leave an answer
You must login or register to add a new answer .