Two queries with WP_Query add a second to complete pagination

What I'm trying to do is create two queries for properties. One will receive regular results based on a regular query. The second request will receive properties that are closely related to the first request. I can run both queries and retrieve all the results using the posts_per_page function, unlimited and without pagination. The problem with adding pagination is that both loops start and display messages on each page.

The page will have 3 from the first cycle, and then 3 from the second cycle.

I tried to combine the two queries into one and show them, but the same results. 3 and 3.

I think I need to add something to make sure that the second loop gets the output after the first. Any thoughts?

Here are my loops (I excluded arguments due to length)

<?php $queryOne = new WP_Query($args); $queryTwo = new WP_Query($args2); $results = new WP_Query(); $results->posts = array_merge($queryOne->posts, $queryTwo->posts); ?> <?php foreach($results->posts as $post) : ?> <?php setup_postdata( $post ); ?> <?php get_template_part( 'property-listing' ); ?> <?php endforeach; ?> 
+4
source share
1 answer

as parse_query relies on post_count , you will need to add two post_counts. In your example, post_count not set. It should work if you fill post_count. Just add this to the end:

 $results->post_count = $queryOne->post_count + $queryTwo->post_count; 

Your complete example:

 <?php $queryOne = new WP_Query($args); $queryTwo = new WP_Query($args2); $results = new WP_Query(); $results->posts = array_merge($queryOne->posts, $queryTwo->posts); $results->post_count = $queryOne->post_count + $queryTwo->post_count; foreach($results->posts as $post) : setup_postdata( $post ); get_template_part( 'property-listing' ); endforeach; ?> 
+3
source

Source: https://habr.com/ru/post/1498428/


All Articles