$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>wp query - One WP_Query that always shows 1 post from category X and 1 post from "not in category X"?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin11PV0评论

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.

Post a comment

comment list (0)

  1. No comments so far