Display Categories in Search Results

I couldn’t find anything pertaining to my question.

How difficult would it be to display the category of search results, without a plugin – if possible.

So it would be something like this:

Category One

Search results from category one

Category Two

Search results from category two

and so on.

Any help would be greatly appreciated. I don’t necessarily need anyone to do the work for me, I’m a PHP/Wordpress beginner, but I’d like to think I’m fairly competent in understanding code.

Unfortunately, I haven’t been able to find something that points me in the right direction when searching google and stackexchange.

2 Answers
2

What you basically want to do is to change the SQL query being done to group the result of the query by category
The code taken from here groups by performing a sort by category. You can place the following code in your themes search.php file

<?php   add_filter('posts_join', create_function('$a', 'global $wpdb; return $a . " INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id) INNER JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id) ";'));
        add_filter('posts_where', create_function('$a', 'global $wpdb; return $a . " AND $wpdb->term_taxonomy.taxonomy = \'category\'";'));
        add_filter('posts_orderby', create_function('$a','global $wpdb; return "$wpdb->term_taxonomy.term_id DESC";'));
    query_posts('');
?>

The rest of you search.php code should be something like

$catid = false;
while ( have_posts() ) { 
  the_post();
  $cat = get post cat id
  if ($cat != $catid) { // switching to posts from new category
    display new categoty title
  }
  display post
}  

The display logic might be more complicated if posts can belong to more then one category as it will be more difficult to decide to which category you are switching

Leave a Comment