wp query – Combine relationship posts with existing wp_query

Question

So I am building a query, and I wanted to see if someone can assist me on a special attempt to infuse two queries.

So here is what I’m attempting to do:

  1. Check if we have any relationship posts, which I can return either an array of IDs or an array of WP_Post objects.

  2. I want to showcase the relationship posts first if they are and then showcase the remaining wp_query posts.

Here is what I’m working with:


Original:

$args = [
    'post_type' => ['work', Report_Type::CPT_NAME],
    'posts_per_page' => 4,
    'post_status' => ['publish'],
];
$query = new WP_Query($args);

Then I’m looping through using a template as shown below:

if ($query->have_posts()) {
    ob_start();
    get_template_part('template-parts/blocks/work-expertise-filter/work-items-template', null, ['query' => $query]);
    $content = ob_get_contents();
    ob_end_clean();
    wp_reset_postdata();
}

So let’s say, that I have get_field('featured_posts') which returns back an ARRAY of IDs (I can check it to ARRAY of WP_Post objects if needed).
This is our return: array(2) { [0]=> int(14244) [1]=> int(14566) } array(4) {

So I attemped to do something like this:

    $args = [
        'post_type' => ['work', Report_Type::CPT_NAME],
        'posts_per_page' => 4,
        'post_status' => ['publish'],
    ];
    $query = new WP_Query($args);

    $all_ids = [];
    if ($query->have_posts()) {
        while ($query->have_posts()) {
            $query->the_post();
            $all_ids[] = array_merge($query->post->ID, get_field('featured_posts'));
        }
        wp_reset_postdata();
    }
    $all_ids = array_unique($all_ids);
    $args = [
        'post__in' => $all_ids
    ];
    $the_query = new WP_Query($args);

But I’m getting no response back, it’s basically: object(WP_Query)#1853 (50) { [“query”]=> array(1) { [“post__in”]=> array(1) { [0]=> NULL } } [“query_vars”]=> array(63) { [“post__in”]=> array(1) { [0]=> NULL }

If someone could help me with a query, it would be much appreciated!

in progress 0
Testtt 2 months 2021-07-30T13:26:36-05:00 0 Answer 0 views 0

Answer ( 1 )

    0
    2021-08-01T07:46:14-05:00

    Because in your second assignment of $args you didn’t specify the post_type

Leave an answer

Browse
Browse