Exclude a Category from your WordPress Search Results

You have a WordPress blog and for one reason or another you want to exclude a certain category from your WordPress search results. It’s pretty simple to exclude or hide a category, or several categories, from your search results and all you’ll need to do is edit your search.php file in your WordPress theme folder.

What you’ll need to do:

Simply open up your search.php file inside your WordPress theme folder and add the following code just above the loop (if (have_posts()) : while (have_posts()) : the_post();). In this example below we are excluding categories with an ID of 8 and 9. You’ll need to find the ID of each category you want to exclude from your search results and include it in the below code.

<?php
$page = (get_query_var('paged')) ? get_query_var('paged') : 1;
$s = get_query_var('s');
query_posts("s=$s&cat=-8,-9&paged=$page");
?>

Adham Dannaway

Web Designer and Front End Developer with a passion for web design, coding, blogging, WordPress and chewing gum. Check out my web design portfolio and follow me on Twitter and Facebook!

Share this post!

Tags: , , ,

  • Share
  • Stumbleupon Cre8ive Commando
    • Share


Have your say!