0

I'm basing my first WP theme off a blank/boilerplate theme which generates the front page post feed using this code (which is generally present in most WP themes):

<?php while ( have_posts() ) : the_post() ?>
<?php get_template_part( 'entry' ); ?>
<?php comments_template(); ?>
<?php endwhile; ?>

I have one category of posts that I don't want displayed in this feed. Knowing that category's ID number and the range of category IDs to include, what's the easiest way to get the feed to exclude one category of posts?

0

I think you can go even simpler in the coding. Just add the below to your functions.php file within your theme folder.

function myFeedExcluder($query) {
  if ($query->is_feed) {
      $query->set('cat','-73,-9');  // categories 73 and 9 are being excluded
      }
  return $query;
}
add_filter('pre_get_posts','myFeedExcluder');

Note particularly that multiple categories can be excluded by separating them with commas.

And here's the codex page on customizing feeds, but it doesn't specifically address this situation.

|improve this answer|||||
1

Use the pre_get_posts action to modify any query parameters before the query is sent to the database.

One of the examples on that Codex page should work for you, just change the cat parameter to the category ID you want to exclude, prefixed with a - minus:

function exclude_category( $query ) {
    if ( $query->is_home() && $query->is_main_query() ) {
        $query->set( 'cat', '-1' );
    }
}
add_action( 'pre_get_posts', 'exclude_category' );
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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