WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to exclude posts from one category. These posts should not show in the sidebar on the homepage only when I explicitly don't want that particular posts to show. In all other cases these posts will be displayed.

I have tried with plugin "WP Hide Post", but that seems not to work with sidebars, only for regular loops. I've checked on hide this post from homepage, and nothing happens.

This is the code I use to display post in homepage sidebar:

<?php query_posts('category_name=events&showposts=3'); ?>
<?php while (have_posts()) : the_post(); ?>
<h2 id="post-<?php the_ID(); ?>">
<a href="<?php the_permalink() ?>" rel="bookmark" accesskey="s"><?php the_title(); ?></a>
<div class="myclass3"><?php wpe_excerpt('wpe_excerptlength_index', 'wpe_excerptmore'); ?>                        </div><BR>
<?php endwhile; ?>
share|improve this question

You can add this code to a new file named wpsites.php and then use the template tag directly in any file or hook it in using a custom function with theme specific or WordPress hooks.

Note: All code can be used in a child theme.


// Your Custom Query Arguments
$args = array(
'category__not_in' => array( 007 ) 
) );

$wpsites_catposts = new WP_Query( $args ); 

// Your Custom Loop
if ( $wpsites_catposts->have_posts() ) {

    echo '<div class="primary-sidebar"><ul>';

while ( $wpsites_catposts->have_posts() ) {


    echo '<li><a href="' . esc_url( get_permalink() ) . '" rel="bookmark">' . get_the_title() . '</a></li>';
    echo '</ul></div>';
} else {
  echo '<div class="primary-sidebar">No posts found for this query.</div>';


Get Template Part

Add this code in any template file. For this question, one of your sidebar.php files.

<?php get_template_part( 'wpsites' ); ?>

Custom Function

Or hook it in if your theme includes action hooks:

add_action ( 'your_themes_before_sidebar_hook', 'exclude_posts_in_sidebar_loop' );
function exclude_posts_in_sidebar_loop() {
if ( is_home() ) {
get_template_part( 'wpsites' ); 

Change the conditional tag in the above custom function to suit your own needs.

Alternative Method For Query

Rather than use the arrays of $args, you can replace Your Custom Query Arguments with this line:

$wpsites_catposts = new WP_Query( array( 
'category__not_in' => array( 007 ) 
) );
share|improve this answer

I'm not sure if this is what you're looking for, but you could just wrap everything INSIDE the while/endwhile with a

<?php if(! in_category( array( [category_id] ) )) ?>

   ....your code here to display the rest of the categories.....

<?php endif; ?>

Just replace [category_id] with the category you are looking to exclude

share|improve this answer
Tnx for answer, but this not what I want.Im searching for solution to manualy enable post from certain category to show or not on sidebar, eg. checkbox in edit post screen say:do not include this on homepage.Plugin "wp hide post" does that, but not work on sidebar, only for regular loop. – user4490 Mar 7 '12 at 9:37

protected by toscho Jun 11 '12 at 11:51

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.