Bruno Fernandes - Web designer / developer

Archive for the ‘Wordpress’ Category

How to exclude categories from homepage, feeds and archives of your wordpress website

Saturday, April 3rd, 2010

During the development of a project using wordpress, i had the need to exclude some categories from the homepage blog, from the archives and from the feeds pages of my wordpress website. During my research to find out a solution, i found several codes but any of them was exactly what i wanted and some of them not even worked. As i had some troubles to solve this problem i decided to share the code, that after all is really simple.

In this post i will explain how to exclude categories from your homepage(the homepage is the page where the blog is showed first – in some cases your homepage can be another page), archives and feeds pages.

You can find bellow the code that you need.

Copy the code bellow and go to your theme functions.php file – you can find it in your theme folder and edit it directly or you can use your admin panel and go into Appearance > Editor, and open functions.php – and paste de code in this file somewhere, can be in the top or in the end of the file, its not important.

Now you must know which categories you want to exclude from homepage, archives and feeds pages. To do it go to your wordpress admin panel, go to Post > Categories and put the mouse hover the category that you want to exclude, in the bottom left corner of your browser should appear a URL and in the end of the URL you can find something like this – “&cat_ID=X” - X is the ID of the category.

If you cant find the ID using the method above you can click in the category and in the URL of the next page the same thing will appear at the end of the URL.

Now just add the categories to the function and its DONE! Now go to your website a see if works. If you have any problem just leave a comment and maybe i can help you.

 add_action('pre_get_posts', 'exclude_categories_' ); function exclude_categories_($toExclude) { global $wp_query; // Use this variable to assign the categories to exclude $categoriesExclude = '-3 -10 -11'; //this is jus an example. Change the ids to fit your needs // this example will exclude the categories assigned to the $categoriesExclude variable //from homepage, archives pages, feeds but not from categories pages if( is_home() || is_feed() ||( is_archive() && !is_category() )) { $wp_query->query_vars['cat'] = $categoriesExclude; } } 

You can change the code as you want to feet your needs, for example if you just want to exclude from feeds page you can use the code bellow:

 add_action('pre_get_posts', 'exclude_categories_' ); function exclude_categories_($toExclude) { global $wp_query; // Use this variable to assign the categories to exclude $categoriesExclude = '-3 -10 -11'; //this is jus an example. Change the ids to fit your needs //you can use one or more categories to exclude just use the same structure // this example will exclude the categories assigned to the $categoriesExclude variable //from feeds page if(is_feed()) { $wp_query->query_vars['cat'] = $categoriesExclude; } }