Reputation: 83
What I am trying to do is create two queries for properties. One will retrieve the regular results based on a normal query. The second query will retrieve properties closely related to the first query. I am able to run both queries and retrieve all results with posts_per_page set to unlimited and no pagination. The problem when adding pagination is that both loops run and display posts on each page.
The page would have 3 from the first loop followed by 3 from the second loop.
I have tried to merge the two queries into one and show them but the same results happen. 3 and 3.
I am thinking that I need to append somehow to be sure that the second loop is getting output after the first. Any thoughts?
Here are my loops(I excluded args because of 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; ?>
Upvotes: 4
Views: 3677
Reputation: 3220
as parse_query
relies on post_count
you'll have to add the two post_counts. In your example post_count
is not set. It should work if you populate the post_count. Simply add this at 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;
?>
Upvotes: 5