wp query - One WP_Query that always shows 1 post from category X and 1 post from "not in category X"?

admin2025-06-04  3

I want to use WP_Query to display 2 posts, the latest from category X, and the latest that is not in category X. Is it possible to create one query that does this, or do I have to query twice? If it's possible, how do I create the one query that gives me the result I want?

This is my arguments array for now:

$args = array(
    'post_type' => 'blog',
    'tax_query' => array(
        array(
            'taxonomy' => 'bloggcat',
            'field' => 'slug',
            'terms' => 'appar',
        )
    ),
    'posts_per_page' => '2',
);

This of course is not what I want, but where do I go from here?

-- Edit --

I managed to find a solution that ends up with one merged query that works for me. I'm not sure this is the best solution so if anyone knows a better way, please contribute.

I this solution, I make two WP_query's with different arguments, then merge them, like so:

$podArgs = array(
    'post_type' => 'blog',
    'tax_query' => array(
        array(
            'taxonomy' => 'bloggcat',
            'field' => 'slug',
            'terms' => 'appar',
        )
    ),
    'posts_per_page' => '1',
);
$notPodArgs = array(
    'post_type' => 'blog',
    'tax_query' => array(
        array(
            'taxonomy' => 'bloggcat',
            'field' => 'slug',
            'terms' => 'appar',
            'operator' => 'NOT IN'
        ),
    ),
    'posts_per_page' => '1',
);

$pods = new WP_Query($podArgs);
$notPods = new WP_Query($notPodArgs);
$blogs = new WP_Query();
$blogs->posts = array_merge( $pods->posts, $notPods->posts );
$blogs->post_count = $pods->post_count + $notPods->post_count;

I want to use WP_Query to display 2 posts, the latest from category X, and the latest that is not in category X. Is it possible to create one query that does this, or do I have to query twice? If it's possible, how do I create the one query that gives me the result I want?

This is my arguments array for now:

$args = array(
    'post_type' => 'blog',
    'tax_query' => array(
        array(
            'taxonomy' => 'bloggcat',
            'field' => 'slug',
            'terms' => 'appar',
        )
    ),
    'posts_per_page' => '2',
);

This of course is not what I want, but where do I go from here?

-- Edit --

I managed to find a solution that ends up with one merged query that works for me. I'm not sure this is the best solution so if anyone knows a better way, please contribute.

I this solution, I make two WP_query's with different arguments, then merge them, like so:

$podArgs = array(
    'post_type' => 'blog',
    'tax_query' => array(
        array(
            'taxonomy' => 'bloggcat',
            'field' => 'slug',
            'terms' => 'appar',
        )
    ),
    'posts_per_page' => '1',
);
$notPodArgs = array(
    'post_type' => 'blog',
    'tax_query' => array(
        array(
            'taxonomy' => 'bloggcat',
            'field' => 'slug',
            'terms' => 'appar',
            'operator' => 'NOT IN'
        ),
    ),
    'posts_per_page' => '1',
);

$pods = new WP_Query($podArgs);
$notPods = new WP_Query($notPodArgs);
$blogs = new WP_Query();
$blogs->posts = array_merge( $pods->posts, $notPods->posts );
$blogs->post_count = $pods->post_count + $notPods->post_count;
Share Improve this question edited Jan 25, 2019 at 10:11 Johan Dahl asked Jan 25, 2019 at 9:46 Johan DahlJohan Dahl 1,3433 gold badges19 silver badges32 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

Queries (both WP_Queries and raw SQL queries) are meant to select objects matching given criteria.

"One post from category and one not from category" is not a description of set of posts - it's description of two posts coming from different sets.

This means you won't be able to select these two posts using only one query...

So yes, you'll have to use two queries for that.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748975617a315307.html

最新回复(0)