WordPress Recent Posts excluding current category

To display recent posts from other categories excluding current category in which current posts resides then following code inside single.php will display recent 5 posts from same category.

Just add following code inside single.php file at the location where you want to display recent posts.

Recent Posts From Other Categories
global $post;
$category = get_the_category($post->ID);
$category = $category[0]->cat_ID;
$myposts = get_posts(array('numberposts' => 5, 'offset' => 0, 'category__not_in' => array($category), 'post__not_in' => array($post->ID),'post_status'=>'publish'));
foreach($myposts as $post) :
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?></a></li>
<?php endforeach; ?>
<?php wp_reset_query(); ?>

If you want to display recent posts from same category in which current post resides then try this code on my other post
WordPress Recent posts from Current / Same Category

Leave a Reply

Your email address will not be published. Required fields are marked *

One Comment