Support » Fixing WordPress » Exclude posts from certain category on blog only

  • I have a custom home page set up and a blog section which currently shows all posts.

    I’d like to exclude ALL posts from category id 9 from showing on the blog page (unless they’ve been assigned to category 9 and another special category which would override this).

    Is this possible? I came across this page but I couldn’t get it to work: http://codex.wordpress.org/The_Loop

    Here’s my current index.php file (note, this is not my home page)

    <?php get_header(); ?>
    
    <article class="main">
    		<?php if ( have_posts() ): ?>
        <?php get_sidebar(); ?>
        <div class="blogIndex">
          <h2>Latest Posts</h2>	
    
        <?php while ( have_posts() ) : the_post(); ?>
    
            <article>
              <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(); ?></a>
              <h2><a href="<?php esc_url( the_permalink() ); ?>" title="Permalink to <?php the_title(); ?>" rel="bookmark"><?php the_title(); ?></a></h2>
              <time datetime="<?php the_time( 'Y-m-d' ); ?>" pubdate><?php the_date(); ?><?php //the_time(); ?></time>
              <?php the_excerpt(); ?>
              <p class="button"><a href="<?php the_permalink(); ?>">read full post</a></p>
            </article>
        <?php 
    
    /*      $i++;
          if ($i % 3) {
          // odd number
          } else {
          echo '<hr>';
          }
    */
        endwhile; ?>
          </div> 
    
        <?php else: ?>
        <h2>No posts to display</h2>
        <?php endif; ?>
    
    </article>
    
    <?php get_footer(); ?>
Viewing 9 replies - 1 through 9 (of 9 total)
  • Try changing:

    <?php if ( have_posts() ): ?>

    to:

    <?php
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $args= array(
    	'cat' => -9,
    	'paged' => $paged
    );
    query_posts($args);
    ?>
    <?php if ( have_posts() ): ?>

    That almost worked ;o)

    Your code successfully excludes that category from my blog page, thanks. But what I’d like to do is have the option of showing certain products from that category on the blog page, IF they have also been assigned to another category which would hopefully override this exclusion.

    This way by default all products in cat id 9 are not shown on the blog, but if the user chooses to show the odd product on the blog page, they can also assign it to another category, for example “show on blog page”.

    Does that make any sense?

    Try:

    $args= array(
    	'cat' => -9,
            'category__in' => array( x ),
    	'paged' => $paged
    );

    where x is the id of the other category.

    I have to admit I haven’t a clue what’s going on, thanks for your patience!

    I’m now getting a “No posts to display” message, here is my current code:

    <?php get_header(); ?>
    
    <article class="main">
    <?php
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $args= array(
      'cat' => -9,
            'category__in' => array(15),
      'paged' => $paged
    );
    query_posts($args);
    ?>
    <?php if ( have_posts() ): ?>    <?php get_sidebar(); ?>
        <div class="blogIndex">
          <h2>Latest Posts</h2>	
    
        <?php while ( have_posts() ) : the_post(); ?>
    
            <article>
              <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(); ?></a>
              <h2><a href="<?php esc_url( the_permalink() ); ?>" title="Click to view: <?php the_title(); ?>" rel="bookmark"><?php the_title(); ?></a></h2>
              <time datetime="<?php the_time( 'Y-m-d' ); ?>" pubdate><?php the_date(); ?><?php //the_time(); ?></time>
              <?php the_excerpt(); ?>
              <p class="button"><a href="<?php the_permalink(); ?>">read full post</a></p>
            </article>
        <?php 
    
    /*      $i++;
          if ($i % 3) {
          // odd number
          } else {
          echo '<hr>';
          }
    */
        endwhile; ?>
          </div> 
    
        <?php else: ?>
        <h2>No posts to display</h2>
        <?php endif; ?>
    
    </article>
    
    <?php get_footer(); ?>

    cat id 15 is the category the user would click to show the post on the blog page.

    Moderator keesiemeijer

    (@keesiemeijer)

    moderator

    Not tested but try this:

    $args = array(
      'paged' => $paged,
    	'tax_query' => array(
    		'relation' => 'OR',
    		array(
    			'taxonomy' => 'category',
    			'field' => 'id',
    			'terms' => 9,
    			'operator' => 'NOT IN'
    		),
    		array(
    			'taxonomy' => 'category',
    			'field' => 'id',
    			'terms' => 15,
    			'operator' => 'IN'
    		)
    	)
    );

    PERFECT!

    Thank you Keesiemeijer!

    Ok – let’s try another approach. Revert back to the original script and change:

    <?php while ( have_posts() ) : the_post(); ?>

    to:

    <?php while ( have_posts() ) : the_post(); ?>
    
    <?php if( in_category('9) && !in_category('15') ) continue;?>

    Even better, thanks Esmi!

    No problem 🙂

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Exclude posts from certain category on blog only’ is closed to new replies.