Categorize Search Results in Wordpress

How to feature categories and organize search results

May 08

22

I’ve always thought that search results pages could be done better. It would be nice to present the data the user wants in a pleasing fashion. In a blog situation or pretty much any situation where the majority of your content is categorized and or tagged, you can very easily present your user a very organized search result based on specific categories and tags.

The theory behind this is simple really. We’re just going to take some common WordPress logic and apply it to our scenario.

For example, if you were to happen to write a blog about developing with WordPress and most of your articles were focused on plugins and themes it would make sense that the majority of searches done on your site would be for content that related to those categories. So why not present the user with the results from those categories specifically at the top of the search results?

Now, as far as I can tell you can’t just use conditional tags like you would in your sidebar or header like I referenced in my previous article on custom headers.
In those example you get to choose what content to query and display. With search you don’t know what is specifically being queried, unless your name is Carnac the Magnificent

So the solution is to use multiple loops. If you need to brush up on what the WordPress loop is you can read more here.

Back to our blog scenario. Here is what we want to do. When somebody searches my site I want to present my users with any results from the plugin and theme categories first and then give them the rest of the search results.

So start off with your normal loop.

<?php if (have_posts()) : ?> <?php while (have_posts()) : the_post(); ?>

Next we’ll check to see if the user’s search query returned anything from our plugin category. The number represents the plugin’s id. You can find this out by going to manage>categories in your WP admin, either hover over the category name in the list and look at the bottom of your browser or click to edit a category and look at the end of the url in your address bar - that is the category id.

<?php if ( in_category('7') ): ?>

At this point you can put in whatever you want to display to the user whether its the title or the post, the excerpt, or the full content. For simplicities sake I’m just going to display the title with a link to its permalink.

<a href="<?php the_permalink() ?>"><?php the_title() ?></a>

Now close out the if statement for your condition.

<?php endif; ?>

And close out the first loop.

<?php endwhile;  ?>
<?php endif; ?> 

Now lets check to see if there are posts returned from the search in the themes category.

We are going to use another loop but in order to get the same set of results from the user’s search query we need to “rewind the posts.” This function allows us to use the same query over again. Here is the code.

<?php rewind_posts(); ?>

And now we just enter the same code for our loop with the conditional category.

<?php while (have_posts()) : the_post(); ?>
    <?php if ( in_category('17') ): ?>
        <a href="<?php the_permalink() ?>"><?php the_title() ?></a>
    <?php endif; ?>
<?php endwhile; ?>

If you have additional categories you would like to feature just repeat.

<?php rewind_posts(); ?>

<?php while (have_posts()) : the_post(); ?>
    <?php if ( in_category('17') ): ?>
        <a href="<?php the_permalink() ?>"><?php the_title() ?></a>
    <?php endif; ?>
<?php endwhile; ?>

Once you are done featuring categories of search results you will probably want to give the user the results that do not fit into your organized categories. We’ll do this by once again rewinding our posts but don’t want to include the results we already have so we need to exclude the categories we featured. The condition we are using checks to see if any posts are in a category and if they are they are skipped the query continues. Here is what the code looks like.

<?php rewind_posts(); ?>

<?php while (have_posts()) : the_post(); ?>
   <?php if (in_category('7')) continue; ?>
   <?php if (in_category('17')) continue; ?>
        <a href="<?php the_permalink() ?>"><?php the_title() ?></a>
<?php endwhile; ?>

Here is the entire thing. I also put in some headings to seperate the content - though you don’t need to do that.

<h3>Plugins</h3>

    <?php if (have_posts()) : ?> <?php while (have_posts()) : the_post(); ?>
        <?php if ( in_category('7') ): ?>
            <a href="<?php the_permalink() ?>"><?php the_title() ?></a>
        <?php endif; ?>					

    <?php endwhile;  ?>
    <?php endif; ?>   

<h3>Themes</h3>

    <?php rewind_posts(); ?>
        <?php while (have_posts()) : the_post(); ?>
            <?php if ( in_category('17') ): ?>
                <a href="<?php the_permalink() ?>"><?php the_title() ?></a>
            <?php endif; ?>
        <?php endwhile; ?>

<h3>Other Results</h3>

    <?php rewind_posts(); ?>
        <?php while (have_posts()) : the_post(); ?>
            <?php if (in_category('7')) continue; ?>
            <?php if (in_category('17')) continue; ?>
                <a href="<?php the_permalink() ?>"><?php the_title() ?></a>
       <?php endwhile; ?>

Posted in Code Snippets, Development

Trackback URL

2 Responses to “Categorize Search Results in Wordpress”

  1. 1

    Hi.. I name is San …

    Thanks for your coding above but can you please advice if i want to display each category in home differently like

    category 1 = only title of post
    category 2 = title with thumbnail
    category 3 = tile , excerpt and thumbnail.

    Thanks

  2. 2

    Hello San,

    To do that you would just need to add the appropriate template tags to your code.

    For example: If you only want the title than just put in the title template tag like in the first example.

    If you want the title with a thumbnail, you’ll probably need to use a custom field to hold the image information and then display it with a template tag.

    The third option is the same theory.

    Once you have rewound your posts, you can include any normal template tag in that particular query.

    Please let me know if you have additional questions or need more clarification.

Trackbacks

Leave a Reply