SimplyExclude Plugin for WordPress
Filed Under: Tags: category, exclude, include, page, plugin, WordPress
Have you ever been working on a client WordPress project, or maybe your own blog and needed to exclude a Post category from the Front page? Or needed to exclude a Page from being included in a Search? Keep reading.
Over the past year I’ve worked on twenty different client projects. Each one a little similar yet each one a little different. On most the client wanted only a certain category listed on the front page. After many checks on the different versions of WordPress, I’ve not found a ’standard’ way of doing this via the default WordPress admin interface. Sure on the Options -> Reading page you can select a Page or Latest Posts to display. But if you need to limit the display on the Front page to say just the News category you are out of luck.
One solution that works very well is to hook into the WP query object to intercept the ‘action’ in WordPress. All it takes is a simple function and filter added to your theme’s function.php file. Something like the following.
// Used to limit the categories displayed on the home page. Simple
function myHomePostsFilter($query)
{
if ($query->is_home)
{
$query->set('cat','1');
}
return $query;
}
add_filter('pre_get_posts','myHomePostsFilter');
If you are not familiar with WordPress filters let me provide some overview. The function takes a single parameter, This is the reference to the WP Query object (see /includes/query.php from the WordPress engine code). Don’t worry about the details as you will not be calling this function directly. This function will be called when WordPress is preparing a query for some display of information via another function call. Inside the function you will see the ‘if ($query->is_home)’. The ‘is_home’ is a built-in WordPress action. for Categories there are 5 total. They are ‘is_home’, ‘is_list’, ‘is_search’, ‘is_feed’ and ‘is_archive’. In our example above I’m only concerned with the ‘is_home’. So if the query is being built for the Home/Front page the if is true so the next line is executed. The next line ‘$query->set(’cat’,'1′);’ sets the query category to the one cat_id I’m interested in. For the example this is cat_id=1. If needed I could have added more than one category as in ‘$query->set(’cat’,'1,5,15,36,285′);’. I could also exclude categories from the category list simply by preceding the cat_id with a minus as in ‘$query->set(’cat’,'-1, -16, -32′);’. The next line returns the query back to the calling function. Then end of function. So far this is just a standalone function. Now we have to tell WordPress we want it to hit our function. This is the last line. A filter in WordPress can be thought of as a ’subscribe’ action. The first parameter of the filter. Tells WordPress you want to subscribe to a certain action. In our case this is ‘pre_get_posts’. See this Codex page on Custom Queries. Scroll down to the section header ‘Category Exclusion’. Our filter basically tells WordPress ‘before you run the query hit our function first’. This give our function to massage the parameters to adjust the query. Done. This works for most versions of WordPress 2.x up to the latest and greatest (2.3.1 as of this entry). Pretty simple.
But sadly this cat_id is hard-coded into the functions.php file. So if this is your blog and you have control of the admin keys to the kingdom you can stop reading. I mean it takes all of two seconds to adjust the value of the cat_ids you wish to include/exclude. But what if you are knocking out a project for a client. A client you don’t really want to hit you up every time they need to exclude/include a category from some WordPress action like ‘keep the category X from showing on the Home page’. You have a choice. You can document for the user how to make edits to the functions.php file and hope they don’t somehow screw the code up.
Or you can look for some plugin that will do this or you. I can tell you I’ve looked through various plugins. Only really found one that came close to making this somewhat automated. And that plugin stopped working at WordPress 2.1. Something about the very complicate queries it was building. After reading through 300 line of it’s code just for the query logic I dropped it. I mean the 8 lines of code I provided above are really all you need for the logic portion to include/exclude cat_ids. Why go through the twisted query logic that didn’t run until after the page was loaded. This in effect caused two queries to be executed.
So I started writing my own. Something simple. I just wanted a list of the categories and the available options or WordPress ‘actions’. Let the user select the action for the category and go. I’ve produced something functional. It’s in the very early stages of work. I’ve also included a section for Pages. for Pages I’ve only worked through the logic for the ‘is_search’ action. For both Categories and Pages the user has the option of including or excluding for the actions.
Here is a screenshot of the SimplyExclude Category admin page. It’s the same for the Pages but just for search action.
So download the SimplyExclude Plugin for WordPress v0.1. It’s version 0.1 0.2 but I plan adding more bling soon. I’ve tested this on the latest 2.3.1 engine only. If you are running this and have trouble please feel free to contact me for help.
Update
After entering this post and sending a similar response to someone on the wp-hackers list it occurred to me that for Pages there should be an option on the Page editor that allows the exclusion of the page from the Search actions. So took some time this evening to update the plugin to version 0.2 (W00t!). So not only will the Pages be listed under the plugin’s options. But along the right sidebar on the Page editor look for the ‘Exclude from Search’ dropdown option. Both options update the same information.
See the screenshot below for what it appears like.

Download SimplyExclude Plugin for WordPress v0.2
Update 2008-04-27
After some comment I took another look at the plugin code. Seems I provided the version for WordPress before 2.3. The new version now works all the way up to WordPress 2.5.1. Also, I’ve relabeled ‘List’ to ‘Archive’. I’ve also added logic for tags inclusion/exclusion. This works the same way as Categories. The tags admin menu only works under WordPress 2.3 and higher.
Download SimplyExclude Plugin for WordPress
Also upcoming is a better Category and Tags display listing. I’ve received some comments that users want to see the display nested similar to other parts of the WordPress admin interface.
Update 2008-07-16
Seems some of the WordPress 2.6 changes do in fact effect the way the plugin stores it’s options. I have a fixed version but due to technical (or accounting) I’m having trouble updating the plugin into the main repository. You can download version 1.7.1 here. I’ll update the plugin into the repository when WordPress gets things worked out.
You can leave a response, or trackback from your own site.



November 27th, 2007 at 2:32 am
Really good and really interesting post. I expect (and other readers maybe :)) new useful posts from you!
Good luck and successes in blogging!
November 27th, 2007 at 2:03 pm
Are you currently accepting advertising on your website codehooligans.com?
Thank you,
Nicole
Marketing Manager
ZTMC, INC.
January 20th, 2008 at 5:22 pm
Hi,
Thanks for your plugin working great !
Though, i can’t get the exclude from list function to work ; it doesn’t change anything wheter i exclude a category or not.
Btw, you may change the version of the plugin
Jonathan
January 28th, 2008 at 3:06 pm
this plugin does not appear to exclude categories from WP 2.3.2. I am using a theme that is widigitzed, perhaps that is why.
January 28th, 2008 at 4:03 pm
Thanks for the reply Robert. I’ll check the plugin. It should work as nothing has really changed since 2.3. But I’ll verify some things. Also, you are write the theme may be the issue. What theme are you using?
February 14th, 2008 at 2:46 pm
I also notice that some of my categories are not showing up under simply exclude, what is up with that?
March 16th, 2008 at 7:03 am
[...] in a certain category but I did not wanted to show that category on my blog frontpage. Using the SimplyExclude Plugin I was able to exclude this category from my [...]
April 24th, 2008 at 8:55 am
Hi - Looks like this plugin isn’t appearing in the admin in WP2.5. Any chance of an update to get it working? Thanks!
April 24th, 2008 at 9:04 am
I’m also trying the functions.php approach in 2.5 but can’t get that working either. To exclude a page from search results, shouldn’t this work?
if ($query-> is_search)
{
$query->set(’page’,'-85′);
}
return $query;
April 24th, 2008 at 9:17 am
Thanks for both comment Scott. I’ve not had a chance to review the plugin under WP 2.5. But good question. I have some work planned on it for this weekend. So look for a new version coming early next week. Thanks again.
April 25th, 2008 at 9:58 am
[...] SimplyExclude Plugin for WordPress | CodeHooligans Uncategorized [...]
April 26th, 2008 at 8:24 am
[...] desativar o plugin, para ver o que estava acontecendo e procurar uma alternativa, até encontrar o SimplyExclude Plugin, que funcionou [...]
April 26th, 2008 at 3:10 pm
[...] recent comment on my site it seems there is another option out there for Category Visibility called Simply Exclude. It looks like he is still having some bugs but his system is a little slicker than mine and it [...]
April 27th, 2008 at 10:10 pm
Scot Hacker »
I just actually looked at the code you pasted into the comment. WordPress does not support the ‘page’ option via the ’set()’ function. This really only works for ‘cats’. To exclude page you should either use my plugin (now updated to 1.2) or install the Search Everything plugin, http://wordpress.org/extend/plugins/search-everything/
April 29th, 2008 at 10:07 pm
thanks a bunch. your freakin’ code pasted in the post broke my site.
April 29th, 2008 at 10:58 pm
Melisa »
What? What version of WP are you running? What other plugins do you have activated? What do you mean ‘code pasted in the post’? there is nothing you should have pasted in. I’m confused. Give me some details and I can resolve this.
April 30th, 2008 at 8:58 am
[...] relieve is here in the form of a mashup up Simple Exclude by Paul Menard and the List feature of Category Visibility by iPeat this is not an official [...]
May 3rd, 2008 at 4:13 am
This is exactly what I’m looking for. Any idea when the issue with the plugin not showing up in the 2.5.1 admin panel might be resolved?
Cheers
May 3rd, 2008 at 5:20 am
Scott »
Thanks for the comment. I just tested this under a fresh wp 2.5.1 install on my laptop. I have no problem seeing the plugin admin panel (Settings -> Simply Exclude). Also for Pages there is an inline content box similar to Page Template, Page Order, etc. names ‘Simply Exclude’.
Are you not seeing the panel at all? Please make sure you have the correct version of the plugin. I had my own confusion with some zip files. The official version of the plugin can be pulled directly from the WordPress plugin repository http://wordpress.org/extend/plugins/simply-exclude/. I think the version from my site were wacked.
If you are still having issues please let me know. And please if possible send me a list of all you other plugins. So that I can setup a similar environment on my local system to make sure there are no plugin conflicts.
Thanks.
May 4th, 2008 at 4:15 pm
Want issues? I have one
Today I’ve install subject plugin on my WP 2.5.1 and have a PHP error on search page (on search attempt):
Warning: Invalid argument supplied for foreach() in x:\home\wp\www\wp-content\plugins\simply-exclude\simplyexclude.php on line 710 .
This error appears on deafult skin with no other pugin activated
May 4th, 2008 at 4:25 pm
Vlad »
Thanks for the issue Vlad. I’m actually working on the plugin as I write this email. I’ll see if I can patch the error. I’ll post back where you can download a new version. I would appreciate you testing this before I submit to the WordPress repository.
Also, can you advise did you setup any page excludes?
May 4th, 2008 at 6:06 pm
No, I catch this bug without any exlcudes
May 5th, 2008 at 7:44 am
Paul, I’ve recieved your mail and replace plugin but unfortunately error is in his place
May 13th, 2008 at 12:50 pm
It’s a great plugin!! Thanks a lot!!
May 16th, 2008 at 12:42 pm
Hi Paul,
Thanks for a great plugin. I’m having a few issues with the excluding of page from the search
I have a page with photos that have titles - these are photos that are used on separate posts on the site. When I search at the moment i.e. ‘Joe Blogs’, I get two results, back - the page and the post that this photo is connected to.
Having told your plugin to exclude the specific page, I still get both.
Do you know why this might be happening? Would certain permalinks affect it? My site is http://www.keylockmanagement.com
Thanks in advance!
Oli
May 16th, 2008 at 12:58 pm
Oli »
Hmm. It should exclude correctly. I’ll look into the Pages code over the weekend.
May 16th, 2008 at 1:37 pm
Thanks.
After a bit of testing, I realised that it’s searching the image data - title, caption, link and permalink of the image.
My permalinks are structured as ../persons_name - I have linked the thumbnails of the images on the Page to this - so I guess when I search persons-name, it sees the permalink, plus the title of the image on the page and the post and gives me both results.
I hope that makes some sense!
May 17th, 2008 at 7:20 am
Hi! Great plugin, I really appreciate your work. I’m having a bit of a problem with the search function, however.
When I try to search I get the following error:
Warning: Invalid argument supplied for foreach() in /simplyexclude.php on line 710
May 17th, 2008 at 8:25 am
Stéphane »
Thanks. I patched this problem reported last week. Can you please download a fresh copy of the plugin from http://wordpress.org/extend/plugins/simply-exclude/ and give it a go. If you are still seeing the issue please let me know and I’ll look into it. Thanks again.
May 18th, 2008 at 3:53 am
Thanks! It’s working fine now. I thought I was using the most up to date version since it was also labeled 1.5.
May 18th, 2008 at 7:17 am
@Stéphane:
That is totally my fault. I was working on some enhancements to the plugin and received another user’s comment similar to your about the PHP warning. So I quickly patched the current version but didn’t update the version number.
May 19th, 2008 at 12:24 am
what an excellent plugin. thank you for your great work!
May 20th, 2008 at 8:48 pm
Great plugin. I may have found a possible bug. When I am in the wordpress admin doing a search for a keyword in posts or pages I get no results. When I deactivate this plugin the search works again. Can someone else duplicate this behavior and recommend a way to correct the problem? Thanks.
May 20th, 2008 at 8:59 pm
@Adrian:
It’s funny because I was hit with that ‘bug’ myself today working on a client site. Good catch. I’m working on a fix to disable to plugin function when using admin-side searches.
May 21st, 2008 at 5:08 pm
I don’t see all my tags or categories listed. I have a few I want to exclude, but they don’t seem to be appearing on my settings section. Thanks
C
May 27th, 2008 at 6:04 am
[...] SimplyExclude Plugin for WordPress | CodeHooligans [...]
June 1st, 2008 at 11:26 am
Great plugin - only I have a question: How can I get the comments for a certain post or page to be excluded from rss and search as well? Right now that doesn’t seem to work.
June 1st, 2008 at 1:17 pm
@Juno: Thanks for the comment. The current version of the plugin does not directly filter comments. It’s something I’ve not thought of. I recently added author filtering so maybe comment filters are next. Look for it in the next release. May be 3-4 days.
June 1st, 2008 at 1:56 pm
Brilliant! I’ll be waiting patiently. It’s great to have a plugin that gives you so much control over your blog-privacy.
June 2nd, 2008 at 12:20 pm
Hi Paul,
Neat plugin, but I have a question. But for some reason the options for excluding from “lists” (like in the sidebar) is completely missing from the interface. Did you remove this functionality? I hope not, because that’s what I really needed.
June 2nd, 2008 at 3:58 pm
@dave: You must have upgraded from an old version. The original plugin used the name ‘Lists’. This was incorrect as it confused many users. The correct name was ‘Archive’. Archives as you might be aware are for categories, tags and authors. The functionality between the two names is the same in regards to the plugin.
If I’m wrong please let me know. I’ll be working on the plugin this week to extend it’s functionality to comments.
June 3rd, 2008 at 11:00 am
Paul,
Actually, this is the first time I’ve tried using your plug-in. And it’s quite nifty.
But when I refer to lists I’m talking about the screenshot above in your blog entry just before your first UPDATE. It shows 5 action names, and describes the “Lists” action as “Visibility on the list of categories on the sidebar.” But when I installed your plugin, there are only 4 action names. “Lists” is missing.
I was just looking for an easy way to exclude certain categories from appearing in my drop down list in the sidebar on my front page. That description seemed to fit the bill.
June 3rd, 2008 at 3:59 pm
Hi Paul,
Thanks for the great plugin, it was just what I was looking for.
Quick problem though, I have a category called “Site News”, I’ve set it to be excluded from searches.. but it isn’t. The posts still turn up.
Do you have any ideas as to what I might be doing to cause this?
Thanks in advance!
June 3rd, 2008 at 4:49 pm
@Sam: Hmm. It should work as advertised. Can you do 2 things for me (ok maybe 3).
1. Make sure you are running the latest version (1.7) downloaded from http://wordpress.org/extend/plugins/simply-exclude/
2. Give me a list of all other active plugins on your site.
3. Double check your settings for my plugin.
June 4th, 2008 at 7:21 am
Sure thing Paul, thanks for the response.
However, the problem with it displaying in the search results I’ve now managed to resolve (I had some code that was clashing with the plugin), but now I have another problem, whereby it’s suddenly started appearing in the RSS feed!
To answer your questions:
1. Yes am running 1.7
2. Currently running: Admin Drop Down Menu, Author Highlight, cforms, Google XML Sitemaps, Gravatar, SEO Title Tag, ShareThis, subscribe2, wp-pagenavi, wp-polls, wp-print, and WP2.3 Related Posts.
3. I’ve doubled checked all settings
Thanks again!
June 4th, 2008 at 9:29 am
Hi! Superb plugin, but major problem: after first usage (excluding some categories), the posts are listes in inverse order (ascending, oldest first). I don’t see any option in wordpress to change this. Do I need another plugin to fix this?
June 5th, 2008 at 7:50 am
Why this plugin doesn’t work on wp_get_archives () function?
June 7th, 2008 at 10:49 am
thank you
great plugin,
June 9th, 2008 at 11:56 am
I am using a plugin called AJAX Calendar: http://urbangiraffe.com/plugins/ajax-calendar/ , and it does not honor the excludes defined in Simply Exclude. Is there a way to get the excludes excluded in this (and other) plugin’s custom query?
June 13th, 2008 at 5:10 am
I’m trying out this plugin, but everytime I exclude something it messes up the order of the posts on the front page putting them in oldest post first instead of newest post first.
June 16th, 2008 at 4:07 pm
[...] Simply Exclude [...]
June 17th, 2008 at 2:09 am
[...] Simply Exclude Plugin or Advanced Category [...]
June 17th, 2008 at 10:22 am
@Simon:
This is the second time someone commented about the issue. The first poster was running on a Russian site.
The details of the plugin are very simple to hook into the WP category/tag filtering. I’m not directly changing the SQL used by WP. I’m simply setting a value of the category to exclude. You can do the same if you have any coding knowledge. Checout the simple section of code at the top of my original post http://www.codehooligans.com/2008/04/27/simply-exclude-plugin/
June 17th, 2008 at 10:24 am
@zaistniejwsieci: Good guestion. The ‘Archive’ as defined by the API I’m hooking into is not the function wp_get_archive() but instead the archive URL as in http://www.codehooligans.com/2008/05/
June 17th, 2008 at 10:26 am
@Jonathan Halter: I’ll need to look at the plugin you mentioned and try to externalize n API or something. Good comment. Thanks.
June 18th, 2008 at 9:29 am
I use a Plugin (FH-More Killer) for showing the full post in the feed no matter if a ‘more’-Link exists. After activating your plugin to exclude a category (on the blog itself as well as in the feed) suddenly the ‘more’ links show up again. Any idea how I can prevent this from happening?
June 18th, 2008 at 9:51 am
@fym: Well I’m really scratching my head. Are you somewhat of a coder? Can you try something for me?
As part of your theme you should have a file named functions.php. This contains misc. functions needed by your theme. Edit this functions.php file and follow the instructions http://zeo.unic.net.my/notes/exclude-category-in-wordpress/ Be sure to update the category values in the 3rd line ‘$query->set(’cat’, ‘-5′);’ to YOUR category ID.
function exclude_category($query) {
if ( $query->is_feed ) {
$query->set(’cat’, ‘-5′);
}
return $query;
}
The above function is basically how my plugin works. I’m just providing a nice interface so you don’t need to edit the functions.php file. Also, please disable my plugin on your site and run a test. Are you still getting the more links?
June 18th, 2008 at 10:45 am
Yeah, “somewhat” of coder.
So, okay. I tried it now on the online blog and it seems to work. Before I just tested it locally, so it seems there was and is something not quite right there with the setup.
I tried putting a modified query_posts in the template files and also tried the method with the functions.php before, but that didn’t work for me. Didn’t make sense and was driving me crazy. Come to think of it, I just tried it all locally. So definitively a problem there, I guess. Will have to look at it.
Well, sorry for any inconvenience
The wood, the trees… you know 
June 26th, 2008 at 12:31 pm
[...] http://www.codehooligans.com/2008/04/27/simply-exclude-plugin/ [...]
June 27th, 2008 at 8:57 am
Thank you for your excellent plugin and for sharing your work. It does exactly what I needed.
PC
June 30th, 2008 at 11:07 am
I installed this plugin and I’m running into a substantial problem: I have excluded one category from the front page … but doing so causes all of my entries to display on index.php in chronological order instead of reverse-chronological order … which means that the oldest post on the site is showing up at the top of the homepage. Any insight as to why and, more importantly, how I can fix it?
July 8th, 2008 at 6:12 pm
@61 same thing is happening to me. This issue renders this filter completely useless.
July 15th, 2008 at 9:17 pm
Does anyone have this working in WordPress 2.6? What *specifically* did you have to tweak?
July 16th, 2008 at 7:12 am
@Daddy: I’ve not tested my plugin in 2.6 yet. But it should work just fine. Though still suspect the issue with the ordering of content may be an issue for some. I still cannot reproduce that on my test sites.
P-
July 16th, 2008 at 9:47 am
Hi
I’m really excited about this plugin because I think it can do exactly what I was looking for.
I just installed the plugin with wp 2.6 Everything shows as it should but the values I enter are not stored neither in the plugin settings not in the “hide from search” on each page.
I’m doing something wrong?
July 16th, 2008 at 9:59 am
@Rodrigo Mejia: Thanks for the comment. I’ve been so bus with client work I didn’t realize WP 2.6 was coming so fast. Last report I saw mentioned an August release. Let me do some QA of the plugin under 2.6 this afternoon. Thanks
July 16th, 2008 at 12:18 pm
Hi Paul
Thanks a lot for the updatede file. It worked perfect with wp2.6!
I just have one more question. Is it possible to use your plugin to make some posts or categories completely unavailable to any anonymous user? I’m trying to have two types of content in my site: some pages and categories for public access and some for registered users.
July 17th, 2008 at 3:38 am
SimplyExclude…
Provides an interface to selectively exclude/include categories, tags and page from the 4 actions used by WordPress….
July 17th, 2008 at 9:37 am
Ok… I just wanted to be sure if it was possible to get what I wanted using this plugin… even with a bit of tweak. Anyway, nice plugin and thanks for your help
July 19th, 2008 at 8:13 pm
Hello. I think you are eactly thinking like Sukrat. I really loved the post.
July 29th, 2008 at 2:16 pm
I’ve been trying to get tag exclusion to work on my homepage for the past week. Nothing but dead ends. Tag__not_in worked, but it had too many side effects. Your plugin works flawlessly. Thanks.
July 29th, 2008 at 5:20 pm
[...] Have you ever been working on a client WordPress project, or maybe your own blog and needed to exclude a Post category from the Front page? Or needed to exclude a Page from being included in a Search? CodeHooligans.com wrote a nice plugin to do this. [...]
August 4th, 2008 at 4:14 pm
I notice that you’ve changed List to Archive. I have attempted to either INCLUDE or EXCLUDE categories from Archive in hopes to control their appearance in the sidebar as part of the category list. I do not see any affect. Am I expecting something that is not meant to occur?
What I’d like to accomplish is to have control over what categories appear or not in the list of categories in the sidebar, plus the front page, search, and feed.
Thanks!
August 4th, 2008 at 4:22 pm
@Matthew:
Thanks for the comment. The plugin is not really coded to handle the display of a listing like the list of category items in the sidebar. The plugin is coded to control the listing of a category archive page for example. Sorry. Though this might be an addition I can make to the plugin. Give me a while. I’m still setting of my development system on a new laptop.
August 4th, 2008 at 6:03 pm
Paul,
Thanks for the reply. This does seem to be an area missing that there not a functional plugin. I definitely like your include OR exclude approach. It’d be real nice to go category-by-category and choose front page, list in sidebar, archive, search, and feed to give full control over category. Likewise, I’m sure for tags though I presently do not use them (once I get control of categories, I plan on looking at tags to see how I might take advantage of them though, to be honest I find the whole cloud approach that seems the current trend to be noisy clutter on a page rather than a functional and orderly feature–maybe because I am always looking for the obscure item buried in the cloud).
I definitely look forward to any update you might do to your plugin. Any workaround to suggest in the interim?
Thanks,
Matthew
August 13th, 2008 at 9:11 am
I have found that each resulting link returns the same files. My includes are set to about a half-dozen categories. That works fine. But the resulting menu links, even though they reference different cat numbers, return the same posts. See at http://www.qualityprojects.com.
FYI, I’ve addressed my 8/4 question by writing simple if/then statements in the sidebar.php file for the categories.
August 14th, 2008 at 2:59 pm
Thanks for your great plugin, It works fine with my JsTheme.
At first, I wont to do this work by a code: ” php query_posts(’cat=-2′); ” But it makes something wrong with my theme. So I am looking for a plugin, and this is it.
Thank you.
August 14th, 2008 at 3:35 pm
Wow, thank you for this plugin! This is exactly what I’ve been looking for. No I have full control over navigating visitors through the pages
August 21st, 2008 at 7:04 am
many thanks for the great plugin. i have it now on my site http://www.per-autopilot-zum-reichtum.de/ live on the deployment. It works fine!
regards tobias