Skip to content
Advertisement

Get most recent posts from specific category

    ?php
$args = array( 'numberposts' => 10, 'order'=> 'ASC', 'orderby' => 'title' );
$postslist = get_posts( $args );
foreach ($postslist as $post) :  setup_postdata($post); ?> 
    <div>
        <?php the_date(); ?>
        <br />
        <?php the_title(); ?>   
        <?php the_excerpt(); ?>
    </div>
<?php endforeach; ?>

I am trying to get the 10 most recent posts from a category named ‘Webinar’. How can this be done? At the moment the code above works in showing 10 posts but not specific from the webinar category.

Advertisement

Answer

This list of arguments is what drives the data that is fetched from the database:

$args = array( 'numberposts' => 10, 'order'=> 'ASC', 'orderby' => 'title' );

You ask for 10 posts, ordered by title in an ascending order. It seems you simply forgot to include the category in your search conditions. Try:

$args = array( 'numberposts' => 10, 'order'=> 'ASC', 'orderby' => 'title', 'category' => 'Webinar');
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement