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.

Read More

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.

Related posts

Leave a Reply

2 comments

  1. 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

  2. You could, in the search.php template page, first get the categories with get_categories.

    Then in the loop, you could loop through the categories to see if the search result has that category. If so, push it under the index of the category. Here you wouldn’t display anything, just build you array.

    Then after the loop you could use a foreach to display the search results ordered by your categories.

    Like this, if a result is in more then a category, it would be displayed for each category.


    UPDATE

    The code would be something like this, inside search.php in your theme.

    1) before the loop:

    $my_cat_array = array()
    $my_cats = get_categories();
    

    Gets the categories.

    2) Inside the loop:

    foreach ($my_cats as $my_cat) {
        if (in_category( $my_cat->term_id)) {
            $my_cat_array[$my_cat->term_id][] = $post;
        }
    }
    

    For each category, it checks if the current post has the category, if it does, it stores it in the array.

    3) After the loop:

    global $post;
    foreach($my_cat_array as $cat_id => $posts_from_category) {
        $current_cat = get_category($cat_id);
        echo "Search result from category ". $current_cat->name;
        foreach($posts_from_category as $post) {
           setup_postdata($post);
           // here would go the display code
        }
     }
    

    Maybe their are other ways to do this, this should work, although I haven’t actually tried it, so there might be some errors. If you do try it and it gives errors, let me know and I will try to help.