1
votes

After merging the results of two wp_queries, I'm trying to use an array of their ID's as an argument for post__in for a new wp_query. But for some reason, I'm getting odd results (the total numbers of posts don't match). As far as I understand I'm not able to convert the array of post-ID's into the right format for post__in (that accepts an array of ID's).

What I've done so far:

echo count($first_query->posts);  // returns 101
echo count($second_query->posts); // returns 201

$together = array_merge((array)$first_query->posts,(array)$second_query->posts);

echo count($together);          // returns 302
var_dump($together);            // returns: array(302) { [0]=> object(WP_Post)#6642 (24) { ["ID"]=> int(41) ["post_author"]=> string(1) "1" ["post_date"]=> string(19) "2005-04-01 05:49:22" ["post_date_gmt"]=> string(19) "2005-04-01 05:49:22" ["post_content"]=> string(0) "" ["post_title"]=> string(21) ...

So far so good. I've casted to array to avoid array_merge returning null, when one of the arrays is empty. Now I'm trying to get an array of ID's to be used in a new wp_query, like this:

$result_args = array(
    'post_type'             => 'post',
    'post__in'              => $ids,    // array(10,11,12 and so on)
    'ignore_sticky_posts'   => 1,
    'post_per_page'         => -1
);

$result = new WP_Query($result_args);

I've been trying to convert $together into an array of integers in several ways:

First try

$ids = wp_list_pluck($together, 'ID');
echo count($ids);        // returns 302
var_dump($ids);          // returns array(302) { [0]=> int(41) [1]=> int(44) [2]=> int(47) [3]=> int(89) ...

After using $ids as an argument for post__in, however, the wp_query $result returns the wrong total of posts.

echo count($result);       // returns 10
var_dump($result_args);    // returns array(4) { ["post_type"]=> string(4) "post" ["post__in"]=> array(302) { [0]=> int(41) [1]=> int(44) [2]=> int(47) ...  int(969) } ["ignore_sticky_posts"]=> int(1) ["post_per_page"]=> int(-1) }

Second try

After adding 'fields' => 'ids' to $first_query and $second_query, they both return arrays of ID's.

echo count($together);    // returns 302
var_dump($together);      // returns array(302) { [0]=> int(41) [1]=> int(44) [2]=> int(47) [3]=> int(89) 

But again, after using the array for post__in the wrong result is returned:

echo count($result);      // returns 10

Third try

Imploding the array of ID's returns a string, that post__in can't handle.

echo count($result);      // returns 0

Maybe I'm looking in the wrong direction and there is something else wrong, but I can't figure out what, so any help would be appreciated.

Resolved

'post_per_page'

Should have been 'posts_per_page' Everything is working correctly now.

1

1 Answers

1
votes

I don't understand why you're not just returning IDs with your original queries, as to avoid all of this conversion in the first place. WP_Query() has a fields parameter which can be used to return an array consisting of only post IDs. In the simplest form, the query would look like the following:

// This query returns an array() of post IDs, rather than full post objects
$args = array(
    'fields' => 'ids',
);
$first_query = new WP_Query( $args );

From that point, you can do a standard array_merge() (both of the queries will result in arrays; so you won't need to cast them as (array)). This merged array will then only consist of post IDs, and can be used directly in post__in for another query (if needed).