Skip links

WordPress get all posts of a specific category

How to get all posts of a specific category or sub category in wordpress?Here I’m going to explain how to get all posts of a specific category or sub category.
You can execute wordpress query to get specific category details as follows.

$category_id = 10;// my category ID
	$args = array(
		 'cat' => $category_id,
		 'post_type' => 'post',
		 'posts_per_page' => 9, // number of posts to a page
		 'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1), // pagination
	);
$cat_query = new WP_Query($args);

This will return all posts of that category query.Now we can get all posts using while loop.

if( $cat_query->have_posts() ) : $count=0;
   while ( $cat_query->have_posts() ) : $cat_query->the_post();
 // ---------------------- display post details here --------
  endwhile;
endif;
// reset query
wp_reset_query();

Her is the full code to get all details with thumbnails of each posts.

$args = array( 'cat' => $category_id,
	 'post_type' => 'post',
	'posts_per_page' => 9,
	'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1),
);
$cat_query = new WP_Query($args);
if( $cat_query->have_posts() ) { 
$count=0;
// your code here
}