WordPress – How To Get Posts by Category ID

This post shows you how to get posts by category ID in WordPress.

To do this, we can use get_posts function as shown right below.

<ul>
<?php
    $category_id = 50; // put your category id here
    $args = array( 'posts_per_page' => 10, 'offset'=> 0, 'category' => $category_id );
    $category_posts = get_posts( $args );

    foreach ( $category_posts as $post ) : setup_postdata( $post ); ?>
        <li>
            <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
        </li>
<?php 
    endforeach;
    wp_reset_postdata();
?>
</ul>

In the above code, we show the recent 10 posts of the category having ID 50 as an example.