[wp-hackers] Exclude Categories From The Loop

Paul Menard paul at codehooligans.com
Fri Oct 5 13:16:31 GMT 2007


I generally exclude/include categories using a simple function I keep  
in the functions.php file. The following example was setup because  
the client only wanted content from the category '1' displayed on the  
home page.

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

The is_home has other sibling functions like is_search, is_list, etc.  
Via the query set() you can also pass the second argument as '-1, -2,  
-3'. I turned this into a simple plugin. It has two admin panels, one  
for categories and one for pages. The pages section is used to simply  
exclude pages from search. But there are other 'actions' where pages  
may need to be excluded. But it's not all that clean. If anyone wants  
to see this let me know. Also this code works well for WP2.2 and  
below. HAve not looked into using this similar for tags.

There was a plugin I found for pre-WP2.1 called Category Visibility.  
It was doing some very complicated MySQL queries and would not work  
with 2.2. This seemed like overkill.



P-




On Oct 5, 2007, at 1:54 AM, Michael D Adams wrote:

> On Oct 4, 2007, at 7:37 PM, Matt wrote:
>> Is it possible to exclude certain catgories from displaying in the  
>> loop,
>> with a plugin?
>
> Do you mean exclude posts from the loop that belong to a specific  
> category?  I don't know if there's a better way to do this now, but  
> there's an old plugin by Ryan that can do this.
>
> http://wordpress.org/extend/plugins/front-page-cats/
>
> There's no UI.  Change $cats_to_show to '-28' (or whatever category  
> you want to exclude), and remove the is_home reference to get it to  
> work everywhere (... or maybe change it to !is_admin...).
>
> Michael
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers



More information about the wp-hackers mailing list