Tag: exclude

Include or Exclude Category for RSS Feed or Search

There are certain times when you need to include or exclude certain categories in the feed or search results. The easiest way to take care of this is to add the following to your functions.php file.

function sdac_remove_from_feed( $query ) {
	if ( $query->is_feed ) {
		$query->set( 'cat',-1 );
	}
        return $query;
}
add_filter( 'pre_get_posts','sdac_remove_from_feed' );

The code above will exclude all posts that are in the category with ID 1 from your feed. If you wanted to exclude everything from the search results – you would change one line:
$query->is_feed to $query->is_search

If you wanted to only show items from the category with ID 1 – you would just need to remove the “-” before the category number.

Note: You can include/exclude items tagged, etc by changing the ‘cat’ to whatever else you would normally use in the query.

Exclude Single Post in WordPress

If you would like to exclude one post from your blog, archive, search results or wherever you need to on your blog, you can do so by putting in a small piece of code within the particular WordPress loop:

The following example will show all posts except for the post with the ID of 179:

<?php if ( $post->ID == '179' ) continue;?>

This is something that I get a lot of requests for and is very useful in a number of situations.

Exclude Single Category in WordPress

If you ever need to exclude a single category from a WordPress page (archives, index, category page, etc) you can easily do so by using a little conditional tag code within the WordPress loop.

The example below will skip over any post that is in the category with the ID of 35:





This can be helpful if you want to not show a particular category in your blog (if you have a category based site setup) – or if you want to hide some categories from your search results.