[wp-hackers] Exclude Categories From The Loop

Paul Menard paul at codehooligans.com
Fri Oct 5 14:24:00 GMT 2007

This was not my original post but I do want to offer my opinion on  
your reply.

You are correct this will 'filter' the post loop items and allow you  
to display just the ones you want.
However, there are a couple issues with this off the top of my head.

1. As you also pointed out this is not very efficient because at this  
point WP have pre-selected all posts. If you only want a single  
category and that category only contain 10 items of the lager 3000  
post item of your blog then the If (in_category()) will be executed  
for 3000 to check.

2. If I set my display items per page count to 5 and considering the  
above information I may have to add even more code to ensure the user  
sees 5 items per page and the paging works correctly.

3. You could do what I've done many times in the past and ignore the  
post/loop provided by WordPress and build your own query in the  
template. But again this is grossly inefficient in my opinion since  
for WP performs the query (+1 database hit) then you hit the database  
again to run your custom query.


On Oct 5, 2007, at 8:35 AM, Kevin wrote:

> Unless something has changed shouldn't the code referenced in the
> codex loop page work?
> http://codex.wordpress.org/The_Loop
> Specificly:
> <?php if (in_category('3')) continue; ?>
> Obviously this isn't optimized from a SQL perspective (you're still
> pull all posts from the DB, just not displaying some of them), but it
> does what was asked for.
> Kevin
> On 10/5/07, Paul Menard <paul at codehooligans.com> wrote:
>> 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
>> _______________________________________________
>> wp-hackers mailing list
>> wp-hackers at lists.automattic.com
>> http://lists.automattic.com/mailman/listinfo/wp-hackers
> -- 
> Kevin Kelley
> http://technogeek.org/
> _______________________________________________
> 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