Sometimes you may need to exclude some categories from wordpress recent posts widget. But unfortunately wordpress has given no option to do it in default recent posts widget. Then how can you control the recent posts widget?
Well, everything is possible if you know how to play with wordpress
. Here is a small php code which can be used to exclude some categories from recent posts widget.
Before going to the code part, check whether you’ve installed WP PHP widget or not. If it in not installed in your site, you can download and install this plugin from here – WP PHP widget. Without this plugin, you can’t get this code to work.
Here is the code,
<ul>
<?php
$recentPosts = new WP_Query();
$recentPosts->query('showposts=5&cat=-category-id'); ?>
<?php while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>
<li><a href="<?php the_permalink() ?>" rel="bookmark">
<?php the_title(); ?></a></li>
<?php endwhile; ?> </ul>
In your widgets menu, add WP PHP widget widget to your sidebar. Then paste the above code into it. Replace category-id with the id of the category that you want to exclude. You can get it in categories menu under posts. (example: 24). Now save the widget. Of course, give this widget a name like “Recent Posts”. You can exclude multiple categories like this,
$recentPosts->query('showposts=5&cat=-category1,-category2,-category3');
Look at my recent posts widget in the sidebar, I’m using the same technique to exclude “Quick Updates” category from recent posts list.
In this image, you can see the difference between default wordpress Recent Posts widget and my Custom Recent Posts widget which is using the above code.
Have questions to ask? Feel free to leave a reply.
Waqas has given an idea of using wp_get_recent_posts function. It can be even better if you use this function.
Here is a sample code which uses, wp_get_recent_posts function,
<ul>
<?php
$args = array( ‘numberposts’ => ’5′, ‘category’ => -category-id, );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
echo '<li><a href="' . get_permalink($recent["ID"]) . '" title="Look '.$recent["post_title"].'" >' . $recent["post_title"].'</a> </li> ';
}
?>
</ul>
Thanks Waqas for pointing it out.
Follow these topics WordPress
Short Link:



11 comments
Skip to comment form ↓
Waqas
December 31, 2011 at 4:19 pm (UTC 5.5) Link to this comment
Why not simply use: wp_get_recent_posts and exclude the category by passing it as an argument to that function?
http://codex.wordpress.org/Function_Reference/wp_get_recent_posts
Prasanna SP
December 31, 2011 at 4:44 pm (UTC 5.5) Link to this comment
Hey Waqas! Thanks for your reply. I’ll update the post.
Chang
January 6, 2012 at 12:56 am (UTC 5.5) Link to this comment
Stumbled on your web site through Reddit. You already know I will be signing up to your rss.
Luko Del Ponte
January 9, 2012 at 2:27 pm (UTC 5.5) Link to this comment
Hy,

just a notice…
code will not work if you copy/paste it in WP-PHP widget,
You put a space between open PHP tag (‘< ?php'). I have to change it to '<?php'
It will be nice just to copy/paste code in PHP widget, but it's more fun like this
Prasanna SP
January 10, 2012 at 9:53 am (UTC 5.5) Link to this comment
Hey Luko! Thanks for notifying this! Syntax highlighter has added that space. I’ve corrected it now.
I need a better Syntax highlighter..
Houseboat kerala
March 24, 2012 at 11:50 am (UTC 5.5) Link to this comment
I just wanted to say that your blog has been really useful.
John
May 28, 2012 at 8:35 pm (UTC 5.5) Link to this comment
Just go to wp-includes /wp-includes/default-widgets.php and modify the querry
-----! $r = new WP_Query(array('posts_per_page' => $number,'cat' => 'HERE YOU CAN PUT YOUR CAT ID!', 'no_found_rows' => true, 'post_status' => 'publish', 'ignore_sticky_posts' => true)); !------Prasanna SP
June 11, 2012 at 9:47 am (UTC 5.5) Link to this comment
Do you know what the Drupal dev team always say, “Never hack the core!”. And it applies to WordPress as well!
ansh
June 7, 2012 at 3:11 pm (UTC 5.5) Link to this comment
have a look at this article:
http://webgeek.elletis.com/how-to-exclude-certain-categories-from-home-page/
LinuXplained
January 18, 2013 at 11:29 pm (UTC 5.5) Link to this comment
Worked for me. Thanks!
jamshid
March 28, 2013 at 2:21 am (UTC 5.5) Link to this comment
Hi, thank you my friend worked for me