Fetching the most recent post from each of 5 distinct WordPress categories

I’m trying to grab the newest post from five different WordPress categories. Here’s what I’ve got so far:

$category_list = array(1, 2, 3, 4, 5);
$recent_posts = array();

foreach ($category_list as $cat_id) {
    $query_args = array(
        'post_type' => 'post',
        'posts_per_page' => 1,
        'cat' => $cat_id,
    );
    $latest_in_category = get_posts($query_args);
    $recent_posts[] = $latest_in_category[0];
}

foreach ($recent_posts as $post) {
    $category = get_the_category($post->ID);
    echo $category[0]->name . "\n";
}

This code is supposed to fetch one post from each category. But when I run it, I get:

Technology
Science
Science
Business
Business

It’s showing duplicate categories instead of five unique ones. What am I doing wrong? How can I make sure I get exactly one post from each of the five different categories?

hey laura, i had a similar prob before. have u tried using wp_query instead of get_posts? it gives more control. also, check if those category IDs actually exist in ur WP setup. sometimes the numbers dont match up with what we expect. good luck!

I’ve encountered a similar issue before, and I think I know what’s going on here. The problem likely stems from how WordPress handles category assignments for posts.

Posts can belong to multiple categories, and get_the_category() returns all categories associated with a post. When you’re echoing $category[0]->name, you’re always getting the first (primary) category, which might not be the one you queried for.

To fix this, you could modify your code to specifically use the category ID you queried:

foreach ($recent_posts as $index => $post) {
    $cat_id = $category_list[$index];
    $category = get_category($cat_id);
    echo $category->name . "\n";
}

This way, you’re ensuring that you’re displaying the category name that corresponds to the ID you originally queried. It should give you the five distinct categories you’re looking for.

Also, make sure your category IDs (1, 2, 3, 4, 5) actually correspond to five different categories in your WordPress setup. If they don’t, you might need to adjust those numbers to match your actual category IDs.

I’d suggest a different approach using WP_Query with tax_query. This ensures you’re fetching posts from specific categories:

$category_ids = array(1, 2, 3, 4, 5);
$recent_posts = array();

foreach ($category_ids as $cat_id) {
    $args = array(
        'post_type' => 'post',
        'posts_per_page' => 1,
        'tax_query' => array(
            array(
                'taxonomy' => 'category',
                'field' => 'term_id',
                'terms' => $cat_id,
            ),
        ),
    );
    $query = new WP_Query($args);
    if ($query->have_posts()) {
        while ($query->have_posts()) {
            $query->the_post();
            $recent_posts[] = get_the_ID();
        }
    }
    wp_reset_postdata();
}

foreach ($recent_posts as $post_id) {
    $category = get_the_category($post_id);
    echo $category[0]->name . '\n';
}

This should resolve your issue and fetch distinct posts from each category.