2

I merged two WP_Queries with the code below, it has pagination setup as well as posts per page in the final combined object but those settings aren't working. My page shows 24 posts, where is it getting this number from and why isn't pagination working?

$args1 = array(
    'post_type' => 'post',
    'posts_per_page' => -1
    );
$query1 = new WP_Query( $args1 );

    $args2 = array(
    'post_type' => 'product',
    'product_tag' => 'home',
    'posts_per_page' => -1
    );
$query2 = new WP_Query( $args2 );

$paged = get_query_var('paged') ? get_query_var('paged') : 1;

$finalargs = array(
  'paged' => $paged,
  'posts_per_page' => 3
);

$wp_query = new WP_Query($finalargs);

$wp_query->posts = array_merge( $query1->posts, $query2->posts );

$wp_query->post_count = count( $wp_query->posts );

echo '<pre>';
//var_dump($wp_query); exit(0);
echo '</pre>';

The output of var_dump is below, so if my wp_query has posts per page and pagination setup why isn't it working?

object(WP_Query)#566 (47) {
  ["query"]=>
  array(2) {
    ["paged"]=>
    int(1)
    ["posts_per_page"]=>
    int(3)
  }
  ["query_vars"]=>
  array(61) {
    ["paged"]=>
    int(1)
    ["posts_per_page"]=>
    int(3)
    ["error"]=>
    string(0) ""
    ["m"]=>
    string(0) ""
    ["p"]=>
7
  • Isn't this essentially the same question as this one?
    – s_ha_dum
    Apr 27, 2014 at 18:20
  • @s_ha_dum no it's not. This has to do with pagination, the other question was asking if I could query two CPT's and filter one of the CPT's
    – Anagio
    Apr 27, 2014 at 18:43
  • But you are trying to solve the same problem, correct? Same taxonomies. Same post types.
    – s_ha_dum
    Apr 27, 2014 at 18:46
  • @s_ha_dum no, in this question the problem is pagination is not working
    – Anagio
    Apr 27, 2014 at 20:57
  • I am not trying to be difficult but I think you have an XY problem with both of these apparently related questions, and I am pretty sure it would help to know what the root problem is.
    – s_ha_dum
    Apr 27, 2014 at 21:36

1 Answer 1

4

I had this exact problem. I just stumbled upon the solution!

In case this helps you, in both my initial queries before the array merge I changed the posts_per_page to be -1 like 'posts_per_page' => -1 instead of 10. When I did this, the pagination links suddenly appeared and worked fine.

I then added 'posts_per_page' => 10 to my final query after the array_merge for it to show 10 per page.

EDIT:

Now looking at your code I see you did that too. Then you should maybe change the way you query as I did, by merging your results array and then doing a query on the merged results. It worked for me and your way did not.

$other_reviews = get_posts( 
    array( 
        'fields'         => 'ids',
        'post_type'      => 'post', 
        'cat'            => 36, 
        'posts_per_page' => -1, 
        'paged'          => $paged, 
        'orderby'        => 'date', 
        'order'          => 'DESC' 
    )
);

$reviews = get_posts( 
    array( 
        'fields'         => 'ids', 
        'post_type'      => array( 'reviews1','review2' ), 
        'posts_per_page' => -1, 
        'paged'          => $paged, 
        'orderby'        => 'date', 
        'order'          => 'DESC' 
    )
);

Then we merge them together:

// merging ids
$post_ids = array_merge( $other_reviews, $reviews );

and construct the combined query with:

// the main query
$query = new WP_Query(
    array(
        'post_type'      => 'any',
        'post__in'       => $post_ids, 
        'paged'          => $paged,
        'orderby'        => 'date', 
        'order'          => 'DESC',
        'posts_per_page' => 10
    )
);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.