Wordpress Plugin - Excluded Categories
The plugin download and related documentation lives at the Wordpress Plugin repository. [LINK]
This plugin is based on the FP Categories plugin by Ryan Boren. However, since that plugin uses a SQL JOIN to access the post’s category field, multiple copies of the same post will appear on the front page when that post is associated with multiple categories. I needed to get around that undesired result. I also wanted to maintain a small list of excluded categories rather than a large list of included categories. So, I dropped the join filter and re-wrote the where filter to use a SQL EXISTS clause to filter out the excluded posts.
[UPDATE: 2007-09-22 2:30 PM PST]
I just posted a new version of the plugin (1.1.0) to the Wordpress Plugin repository. This newest version is now compatible with Wordpress 2.3 RC1. As soon as the stable version is published on Monday I will double check compatibility ASAP.
Sean,
Your plugin didn’t work directly out of the box for me. The SQL statement was broken. I think I’ve got a slightly more elegant solution though. Take a look and feel free to include. I’m sure this will get mangled, but here it is. Email me if you want me to send it to you as an attachment.
function fpe_where($where) {
// Change the $cats_to_exclude string to the category id you do not want to appear on the front page.
// Example: $cats_to_exclude = ‘1 2 3 4′;
$cats_to_exclude = ‘26′;
global $wpdb, $wp_query;
if (! $wp_query->is_home || strlen($cats_to_exclude) == 0) {
return $where;
}
$where .= ” AND $wpdb->post2cat.category_id NOT IN (” . $cats_to_exclude . “)”;
return $where;
}
function fpe_join($join) {
global $wpdb, $wp_query;
if (!$wp_query->is_home) {
return $join;
}
$join .= “LEFT JOIN $wpdb->post2cat ON $wpdb->post2cat.post_id = $wpdb->posts.ID “;
return $join;
}
add_filter(’posts_join’, ‘fpe_join’);
add_filter(’posts_where’, ‘fpe_where’);
Said by Jason April 10, 2007 at about 7:46 pm
Hi Jason,
Thanks for the feedback. I will review the code and possibly modify the source. The reason I didn’t go with a SQL JOIN statement is that for posts where you assign more than one category, the post itself will be returned in the record set multiple times.
So I can troubleshoot, can you describe your wordpress environment (version, database version, and OS)? Also, do you happen to have the error message that was produced using my code?
Thank you!
Sean
Said by seanosteen April 11, 2007 at about 11:31 am
Aha! Here’s why it didn’t work for Jason. MySQL 4.0 and earlier versions do not support sub queries, so the NOT EXISTS or NOT IN syntax is invalid. If you are using 4.0 (Wordpress requires at least 4.0) you will have to use Jason’s hack with the SQL JOIN, or the version written by Ryan Boren. This code will only work on MySQL 4.1 and above. Thank you to Jason York for discovering the flaw.
Said by seanosteen April 11, 2007 at about 2:34 pm
I am not able to get this to work. I get a PHP error/waring, on attempting to activate the plug-in and no category gets excluded, of course. error is:
Warning: Cannot modify header information - headers already sent by (output started at /home/content/j/a/l/jaluo/html/wp-content/plugins/front_page_excluded_cats.php:56) in /home/content/j/a/l/jaluo/html/wp-includes/pluggable.php on line 275
Said by mcowiti April 23, 2007 at about 9:10 am
I posted v1.0.3 on the Wordpress Plugin directory today. I removed the extra carriage returns that were causing Apollo and others’ error message. If you are still seeing errors or unexpected behavior, please let me know.
Thanks,
Sean
Said by seanosteen April 23, 2007 at about 1:18 pm
hoi 2 you,
I have wrote about your plugin before and was quite satisfied, but just recently I get this error message:
WordPress database error: [Not unique table/alias: 'wp_post2cat']
SELECT SQL_CALC_FOUND_ROWS distinct wp_posts.* FROM wp_posts LEFT JOIN wp_post2cat ON (wp_posts.ID = wp_post2cat.post_id) LEFT JOIN wp_post2cat ON wp_post2cat.post_id = wp_posts.ID WHERE 1=1 AND wp_post2cat.category_id IN (20) AND (post_type = ‘post’ AND (post_status = ‘publish’ OR post_status = ‘private’)) AND wp_post2cat.category_id NOT IN (20, 27) GROUP BY wp_posts.ID ORDER BY post_date DESC LIMIT 0, 1
Can you shed some light in there what is going wrong?
My mysql knowledge is far exceeded by this at the moment.
thanks in advance,
jez
Said by jez April 30, 2007 at about 3:46 am
Thanks so much guys! Worked perfectly on my 2.2 install.
Said by luke June 9, 2007 at about 2:09 pm
Hello seanosteen,
Can you confirm that this plugin is working on WP 2.2?
Said by Ibnu Asad June 11, 2007 at about 7:56 am
This plugin is currently working on Wordpress 2.2. I just have not updated the Wordpress directory yet.
Said by seanosteen June 11, 2007 at about 8:21 am
Working on an upgrade to my site, just went to 2.2.1, getting this error:
WordPress database error: [You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Post) ORDER BY post_date DESC LIMIT 0, 10' at line 1]
SELECT SQL_CALC_FOUND_ROWS distinct wp_posts.* FROM wp_posts LEFT JOIN wp_post2cat ON wp_post2cat.post_id = wp_posts.ID WHERE 1=1 AND (post_type = ‘post’ AND (post_status = ‘publish’ OR post_status = ‘private’)) AND wp_post2cat.category_id NOT IN (User Post) ORDER BY post_date DESC LIMIT 0, 10
Test site for upgrade is here, I will leave it like that for a day or two so you can see what’s happening.
Said by Lynne June 28, 2007 at about 1:49 pm
Blegh. Nevermind, I am an idiot. Category ID, NOT the name. *smacks self*
Said by Lynne June 28, 2007 at about 1:51 pm
If you change these first few lines to the following:
// Change the $front_page_cat string to the category id you do not want to appear on the front page.
// Example: $front_page_cat = ‘3′;
$front_page_cat = ‘3′;
global $wpdb, $wp_query;
if (! $wp_query->is_home || strlen($front_page_cat) == 0) {
return $where;
}
$where .= ” AND $wpdb->post2cat.category_id IN (” . $front_page_cat . “)”;
Then only the post you mark for the front page will go there. I found this much more useful because I can have a post in multiple categories but unless I mark it for the front page it won’t appear there. This is of course a matter of preference, but I thought other users might like this idea.
Said by jcam August 3, 2007 at about 11:44 am
Thanks jcam!
That’s a great alternative.
Cheers,
Sean
Said by seanosteen August 3, 2007 at about 12:01 pm
thank you very very much for this plugin. you are my hero
Said by peex August 8, 2007 at about 4:30 pm
Anyway, is it possible to use this plugin also to exclude certain category from date-based archive page?
So, for example, in January 2007, the entry listing will be the same as as the main index page? Thanks!
Said by Thomas August 10, 2007 at about 8:41 am
Hi Thomas,
If you open the plugin in a text editor, look for an if statement that tests for something like ‘$wp_query->is_home’. You could probably remove that entire statement block, and this plugin would limit the categories for all of the pages, not just the main page. I would offer you a code sample, but I’m away from my office at the moment.
Thanks,
Sean
Said by seanosteen August 10, 2007 at about 11:40 am
WordPress database error: [Unknown column 'Local' in 'where clause']
SELECT SQL_CALC_FOUND_ROWS distinct wp_posts.* FROM wp_posts LEFT JOIN wp_post2cat ON wp_post2cat.post_id = wp_posts.ID WHERE 1=1 AND (post_type = ‘post’ AND (post_status = ‘publish’ OR post_status = ‘private’)) AND wp_post2cat.category_id NOT IN (Local) ORDER BY post_date DESC LIMIT 0, 5
I get the error above when using your plugin. Think you can help, Thanks, A
Said by andy August 12, 2007 at about 12:36 pm
Hi Andy,
When you add your post category to the plugin, it must be an integer category id not the category name. You can view the category id in the leftmost column in your blog’s category management page (…/wp-admin/categories.php).
Hope this helps,
Sean
Said by seanosteen August 12, 2007 at about 3:19 pm
If the post have two or more categories, it’s posted on front page if I don’t exclude all this categories. It’s possible to modify the script in order to exclude the post if only one of this categories is excluded?
Thanks
Alessandro
Said by Alessandro September 5, 2007 at about 10:25 am
I have an old version floating around that queries the MySQL database in a different manner. It uses an EXISTS() test rather than a SQL join. I changed the code to use the JOIN statement in order to be compatible with earlier versions of MySQL. As long as you have MySQL 4.1 or later and are using Wordpress 2.2 or later, then my old version will work for you. If you are interested, shoot me an email and I’ll dig it up and email it to you.
Thanks,
Sean
Said by seanosteen September 5, 2007 at about 12:02 pm
Hi there,
I’m guessing it’s an easy modification to exclude from archive pages as well but my php isn’t very… well it just isn’t!
How do I declare is_home OR is_archive in that first wp_query ???
Thanks for your plugin - any support you can give on a free bit of software would be outstanding!
Said by Gareth September 11, 2007 at about 4:05 am
Prescribe cialis online.
Cialis online. Order cialis online. Cialis buy cialis order cialis buy cialis online. Cialis best price buy online. How to buy cialis online. Buy cialis generic online. Buy cialis mg x in us online pharmacy.
Trackback by Link buy cialis online. September 17, 2007 at about 3:03 pm
Will this plugin be updated to work with WP 2.3? Or does anyone know an equivalent?
Said by k3 September 22, 2007 at about 3:45 am
I have not had the time to keep up with the betas. But I will try to do an update against 2.3 RC1 within the next week.
Thanks,
Sean
Said by seanosteen September 22, 2007 at about 10:51 am
I just posted version 1.1.0 on the Wordpress subversion system. It should be available for download within the hour. This new version is compatible with Wordpress 2.3’s new Category/Tag taxonomy system.
Said by seanosteen September 22, 2007 at about 2:33 pm
Hi, the plugin doesnt work for the final 2.3 version.. is it me or is it true?
Said by Anders September 25, 2007 at about 11:27 pm
Hi Anders,
Version 1.1.0 should work with the Wordpress 2.3 final. I have it running on several installations of WP 2.3. Are you getting any error messages?
Thanks,
Sean
Said by seanosteen September 26, 2007 at about 6:37 am
oops, sure enough, new posts do not appear to be filtered. I’ll have to troubleshoot further and get back to you.
Said by seanosteen September 26, 2007 at about 6:55 am
Looking forward to the final fixed release. Do you think an admin panel to select categories will be in a future version?
Also, could you please contact me with the EXISTS() version, if it is still workable on 2.3? Not having to exclude every sub-cat would save me a lot of time.
Said by Rirath September 26, 2007 at about 12:30 pm
I just posted version 1.1.1 which now checks the correct database field for the category (now the ‘term’) id. Give it a go folks!
Said by seanosteen September 26, 2007 at about 1:48 pm
After addition tags to all posts plugin stopped acting. (ver 1.1.1, Wordpress 2.3)
Said by bolek September 26, 2007 at about 3:15 pm
Sorry bolek, I don’t understand what broke? Another plugin called “tags to all posts” or the tags already on all of your posts. Could you reframe or rephrase the problem. If available, can you send me any error messages?
Thanks,
Sean
Said by seanosteen September 26, 2007 at about 4:36 pm
Thank you so much for this! It’s very easy to set up and does exactly what it’s supposed to do. I really needed something like this after the last category exclusion plugin I was using failed to cooperate with WP 2.3!
Said by John September 29, 2007 at about 5:49 pm
Noticed that the categories I had hidden are now showing on the main page again. Interestingly, this occurred now that it is October; all September, everything was fine. Is this being experienced by anyone else?
Thanks!
Said by Dysantic October 1, 2007 at about 10:58 am
Ignore my previous. This appears to be linked (no pun intended) to my Blogroll. That was the only change I made today, and deleting all my links fixed the issue. Must be one of the entries that was screwing it up, as I have added half back, and the issue has not risen again.
Happy October 1st!
Said by Dysantic October 1, 2007 at about 12:13 pm
I’m using 1.1.1 on Wordpress 2.3, and I seem to have encountered a bug. It works on one category but not on another. I don’t know if it’s relevant, but the category it fails to exclude was created after the wordpress upgrade, unlike the category it is excluding.
Thanks for all the hard work.
Said by RobotDan October 1, 2007 at about 1:38 pm
I really thought I posted a comment here but don’t see it… I installed this today and it does not seem to be working for me. I assume the only thing I change is the string in the plugin file itself? That’s what I did, and as you can see if you go to my web page right now, that particular category is showing itself.
Said by Asylum October 1, 2007 at about 2:24 pm
Hi Asylum,
I found that with the schema upgrade from WP2.2 to WP2.3, some of my cateogry IDs (the integer next to the category name in the wp-admin control panel) had changed. Did any of your category IDs change? And if so, have you added those new category ids to the exclusion string?
Let me whether or not that fixes the problem.
Thanks,
Sean
Said by seanosteen October 1, 2007 at about 4:53 pm
Hi RobotDan,
Two things that we’ve noticed so far:
1. Depending on how your tags and categories were created before you upgraded to WP2.3, the category ID may be reassigned a new integer after the upgrade. Are you sure that both the old and the new category id integers are in excluded categories string in the plugin?
2. If the new posts are somehow now associated with two or more categories, you will need to add all of the categories to your list before that post will be suppressed. This was for compatibility reasons with older versions of WP and MySQL. I used to have a version that formatted the SQL statement differently using an EXISTS(subquery) syntax, but I haven’t had time to try to upgrade it to the WP2.3 schema yet.
Double check these two things above, and let me know if that fixes your issue.
Thanks,
Sean
Said by seanosteen October 1, 2007 at about 5:00 pm
Well, I installed this plugin after I made the upgrade to 2.3. I didn’t bother to check my database, but added the adjacent numbers to each… so, the category to be excluded was 7, so I added 6 and 8, and now the post has dropped off the front page. So, I think you are onto the solution to the problem, though I’m curious as to why if my category ID changed it isn’t showing up in the console. Thanks for the solution, though. For my purposes at this point, its all I need.
Said by Asylum October 1, 2007 at about 5:00 pm
I’ve just tried to replicate my problem, so I created a new category and a new article. It seems the answer is very simple… if I add tags to the post it shows, if I don’t it is excluded! I hope this helps you to fix it.
Said by RobotDan October 3, 2007 at about 1:51 pm
RobotDan,
Yep, that’s exactly the case for me, too. I created a post in the correct category that was being excluded before, but added tags, and the post shows on the front page. A new test without tags and the category is excluded. Consider yourself corroborated.
Said by Asylum October 4, 2007 at about 7:07 am
Thanks Sean, seems like a great plugin. Unfortunately, I cannot get it to work. I changed the php file to the correct category number and activated the plugin - nothing happens. Not even a MySQL error.
My server is running on PHP MySQL library version 5.0.26 and MySQL server version 4.0.15. Might this be the reason? Is there a way to find out whether the mysql gets executed properly?
Thanks,
Matt
Said by matt October 7, 2007 at about 11:58 am
I get the exact same behavior - it works great until you add tags to a post and then they’re no longer excluded.
Said by Dave October 24, 2007 at about 12:19 pm
Is there a way to set this to only show a few catagories? Like, say you have 90 catagories, and you only want 3 of them to show on the main page. Is there a way to do that? Or do you have to specify each and every one to disallow?
Said by adam November 4, 2007 at about 7:11 pm
Thanks bro for this great plugin i make us of
Said by Kutsalodun November 15, 2007 at about 6:31 pm
I have the same behaviour as a coup[le of people above, the category is only excluded IF the post has not tags, if I give it tags it appears on the frontpage.
Any fix for this yet?
Said by Stephen Mansell November 19, 2007 at about 2:23 am
It’s not clear from the plugin description whether a category can be excluded from the RSS feed as well. This functionality is built into the Ultimate Category Excluder plugin, which causes some minor conflicts with WordPress 2.3.1. Any possibility of you merging the two plugins? The author of that plugin (Kyle Slattery) is looking for someone to take over since he apparently doesn’t have time to keep up the code.
Just a thought. Thanks for listening.
Said by Maria November 26, 2007 at about 5:13 am
i am trying to exclude two categories i.e. 1 and 15 but not working when edit it shows error in 18 line or 14 line
can any one set a example how can i exclude from front page.
regards
Said by vikas December 12, 2007 at about 9:19 am
Hello,
I have problem with this plugin. At my pages http://czechbracelet.info/ I try to use this plugin.
I modified plugin: $cats_to_exclude = ‘48′;
But this category (Promo - http://www.czechbracelet.info/?cat=48) is shown at home page.
Can you help me?
Thank you for reply.
Edie
Said by Edie December 28, 2007 at about 1:16 pm
I think I solved the problem with the tags.
If you want to try, here’s how I modified “fpe_where”:
function fpe_where($where) {
// Change the $cats_to_exclude string to the category id you do not want to appear on the front page.
// Example: $cats_to_exclude = ‘1, 2, 3, 4′;
$cats_to_exclude = ‘1, 3′;
global $wpdb, $wp_query;
if (! $wp_query->is_home || strlen($cats_to_exclude) == 0) {
return $where;
}
if (empty($wpdb->term_relationships))
{
$where .= ” AND $wpdb->post2cat.category_id NOT IN (” . $cats_to_exclude . “)”;
}
else
{
$where .= ” AND $wpdb->term_taxonomy.term_id NOT IN (” . $cats_to_exclude . “)”;
$where .= ” AND $wpdb->term_taxonomy.taxonomy=’category’”;
}
return $where;
}
Said by Paolo January 9, 2008 at about 8:46 am
Cheap … phentermine cod url.
Phentermine cod best online pharmacy. Phentermine no prescription cod.
Trackback by Phentermine cod. January 10, 2008 at about 10:20 am
Paolo, that gave me a syntax error. What did you change? It looks to me like the end quote on a couple of places got changed to something else, or did this just happen when I copied/pasted?
I decided not to fool with it until Sean makes an update. For now, I’ll go without tags because I rely on this plugin. I can add tags later.
But definitely, if you add tags to any post, the post then shows up on the front page regardless of the category. If you remove tags, the plugin works flawlessly. (Mighty fine plugin, btw…I love this one!)
Said by Juli January 10, 2008 at about 1:29 pm
Just giving you a heads up that I’m using your plugin and modified the source code a little bit.
Instead of excluding certain categories, I modified the source to include only the categories within the ‘excludes’ list.
Said by Bobby W. January 10, 2008 at about 5:29 pm
Make peace, not war!
Said by enzypegem January 11, 2008 at about 4:49 am
Hi Sean-
Your plug in seems to be just what I needed to be able to direct people to different pages on my Wordpress blog. I downloaded it, ftp’d to my server, and it’s been activated. Question is (verrrry newbie here) how do I use it? I went to do a new post, but I’m not sure how to exclude the category from the front page.
Thanks in advance-
Rich
repstein@residentialproperties.com
Said by Rich January 13, 2008 at about 7:19 pm
Hi Sean,
Thank you for the great plugin. I can’t get it to work for some reason. I inserted exactly what you have posted and changed the category ID… notta. We are using Mysql v 5.0.22 and WP 2.3.2
Any advice appreciated
Best to you in 2008
Frank
Said by Frank January 22, 2008 at about 1:26 pm
Hi Sean,
I found your plugin simple and effective but thought it could use an options page. I added to it a little function to create an options page so users don’t have to dig into the PHP source. The modified code is at:
http://oregonstate.edu/~hessro/front_page_excluded_cats.phps
Best.
Said by Rob February 1, 2008 at about 11:03 pm
hi,
i use version 1.1.1 and the php file Rob mentioned above.
I recently upgraded from WP 2.2.1 to 2.3.3, now the category filter does not work anymore.
any suggestions?
cheers,
chris
Said by chris February 14, 2008 at about 1:23 am
Hi Chris,
The jump to Wordpress 2.3.* introduced a data schema change where categories and tags were lumped into the same “taxonomy” system. As a result, this plugin doesn’t work correctly when tags are also added to the post. It’s a known issue and I just haven’t had the time to dig into the database SQL queries to get it figured out. If there’s someone who is interested in debugging this, or possibly taking over the maintenance of this plugin, please let me know.
Thanks,
Sean O’Steen
Said by seanosteen February 14, 2008 at about 9:30 am
Sean
Thanks a million. Very useful.
Used it on my site here:
http://blog.goldassets.co.uk
Isn’t collaboration great? You should use Rob’s options page update a few posts above.
Cheers
Ed
Said by edzillion March 4, 2008 at about 10:41 am
Tramadol.
Tramadol for dogs side effects. Tramadol.
Trackback by Tramadol. March 5, 2008 at about 4:18 pm
Hey,
is it possible to exclude the articles in category XXX also from the archive pages resp. to “remove” the category as shown entry in the category list?
Thanks
Said by Tobi March 5, 2008 at about 11:48 pm
Hi,
For some reason, i can’t hide more than 1 category.
the plugin is set to hide 1 category and it works very good, but when i’m trying to add another category ID to the code (12,34 etc) it doesn’t work and shows the second category in the front page.
Any idea what i’m doing wrong?
Said by Erez March 22, 2008 at about 5:36 pm
Hi there,
is this compatible with WP 2.5? I am not finding the place to “not include” page in menu.
Thanks!
Said by David Stembridge March 31, 2008 at about 11:15 am
To correct the TAGS-bug we need to replace this line in fpe_where():
$where .= ” AND $wpdb->term_taxonomy.term_id NOT IN (” . $cats_to_exclude . “)”;
…to this:
$where .= ” AND $wpdb->term_taxonomy.term_id NOT IN (” . $cats_to_exclude . “)”;
$where .= ” AND $wpdb->term_taxonomy.taxonomy=’category’”;
This solution is sugested earlier and it works fine. You just need to correct quotation marks after copy&paste this code to your editor (it because this blog replaces simple quotes with pretty ones).
Thanks for plugin (Sean O’Steen) and thanks for patch (Paolo)
Said by Oleg Satov April 2, 2008 at about 3:30 am