Wednesday, 17th Sep 2014

«

»

Print this Post

How to exclude categories in wordpress recent posts widget?

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.

Two different Recent Posts Widgets

Can you notice the difference between "Recent Posts" widget and "Custom Recent Posts" widget?

Have questions to ask? Feel free to leave a reply.

Notice

Update: Even tighter code

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.

Short Link:

About the author

Prasanna SP

Student | WordPress Dev | Tech Blogger | Interested in GNU/Linux, FOSS, PHP, Drupal, WordPress, Ethical hacking, Photography, Painting, Literature etc..

11 comments

Skip to comment form

  1. Waqas

    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

    1. Prasanna SP

      Hey Waqas! Thanks for your reply. I’ll update the post.

  2. Chang

    Stumbled on your web site through Reddit. You already know I will be signing up to your rss.

  3. Luko Del Ponte

    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 :)

    1. Prasanna SP

      Hey Luko! Thanks for notifying this! Syntax highlighter has added that space. I’ve corrected it now. :-)

      I need a better Syntax highlighter.. ;-)

  4. Houseboat kerala

    I just wanted to say that your blog has been really useful.

  5. John

    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)); !------

    1. Prasanna SP

      Do you know what the Drupal dev team always say, “Never hack the core!”. And it applies to WordPress as well! :-)

  6. LinuXplained

    Worked for me. Thanks!

  7. jamshid

    Hi, thank you my friend worked for me

Leave a Reply

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

Please put your code snippet between <code> and </code>. Comment moderation is in use. Please do not submit your comment twice -- it will appear shortly.