Add a custom query to fetch and display the latest posts from a specific category

Tom R Dec 18, 2024 Custom Queries
How can I show the newest articles from a specific section of my blog on my homepage?
What is the custom code snippet to modify the WordPress loop to fetch and display the latest posts from a specific category?
Andy answered Dec 18, 2024

Custom Solution

Method 1: Using WP_Query

Add this code to your theme's template file (like front-page.php, home.php, or index.php):

This code fetches the 5 latest posts from category "news":

<?php
$custom_query = new WP_Query(array(
    'category_name' => 'news',    // Replace 'news' with your category slug
    'posts_per_page' => 5,        // Number of posts to display
    'orderby' => 'date',
    'order' => 'DESC'
));

if ($custom_query->have_posts()) :
    while ($custom_query->have_posts()) : $custom_query->the_post();
?>
    <article class="post">
        <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
        <?php the_excerpt(); ?>
    </article>
<?php
    endwhile;
    wp_reset_postdata();
endif;
?>

Method 2: Using Category ID

If you prefer using category ID instead of slug:

<?php
$custom_query = new WP_Query(array(
    'cat' => 4,                   // Replace 4 with your category ID
    'posts_per_page' => 5,
    'orderby' => 'date',
    'order' => 'DESC'
));

if ($custom_query->have_posts()) :
    while ($custom_query->have_posts()) : $custom_query->the_post();
?>
    <article class="post">
        <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
        <div class="meta">
            <?php echo get_the_date(); ?>
        </div>
        <?php the_excerpt(); ?>
    </article>
<?php
    endwhile;
    wp_reset_postdata();
endif;
?>

Method 3: Function for Reusable Code

Add this to your theme's functions.php:

function display_category_posts($category_slug, $post_count = 5) {
    $custom_query = new WP_Query(array(
        'category_name' => $category_slug,
        'posts_per_page' => $post_count,
        'orderby' => 'date',
        'order' => 'DESC'
    ));

    if ($custom_query->have_posts()) :
        echo '<div class="category-posts">';
        while ($custom_query->have_posts()) : $custom_query->the_post();
            ?>
            <article class="post">
                <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
                <?php the_excerpt(); ?>
            </article>
            <?php
        endwhile;
        echo '</div>';
        wp_reset_postdata();
    endif;
}

Then use it in any template file:

<?php display_category_posts('news', 5); ?>

Plugin Solutions

If you prefer using a plugin, here are reliable options:

  1. Display Posts Shortcode - Use shortcodes to display posts from specific categories
  2. Category Posts Widget - Adds a widget to display posts from selected categories
  3. Post Grid Elementor Addon - Works with Elementor page builder to create customized post grids

These plugins offer user-friendly interfaces and additional customization options without coding.