Multiple Search Results Pages within One WordPress Site

Please note: This post is from 2009, and the information contained may now be outdated.

On a client site I’m currently developing in WordPress, I’m having to add the client’s existing articles to the new site, as well as his old blog. I wanted them to both exist as Posts, but to be separated within the site. Most of that was straightforward – by assigning articles and blog posts to their own Post Categories, I was able to only include, say, Posts from ‘Category #1′ on the Articles page, and Posts from ‘Category #2′ on the blog page.

Where I came a little unstuck was the Search feature in WordPress. No matter which page I searched from, I would be given the relevant results from all Posts, not just the Category in question. So here’s how I went about fixing that…

Firstly, I added the following Search form to my sidebar:

<form id="searchform" action="<?php bloginfo('home'); ?>/" method="get">
<input id="s" maxlength="96" name="s" size="18" type="text" value="Search" />
<input name="site_section" type="hidden" value="blog" />
<input id="searchsubmit" class="hidden" type="submit" value="Search" />
</form>

Note the third line, with the hidden input (that’s the third line if you paste that into a code editor, not what shows up here). On the sidebar for the blog, I gave that input field a value of ‘blog’; for the sidebar on the articles page, I gave it a value of — yeah, you guessed it — ‘articles’.

Then, create a file named ‘search.php’, and paste the following into it:

/* Template Name: Search Results */
$search_refer = $_GET["site_section"];
if ($search_refer == 'blog') { load_template(TEMPLATEPATH . '/blog-index.php'); }
elseif ($search_refer == 'articles') { load_template(TEMPLATEPATH . '/articles-index.php'); };

The important part here is, again, the third line. This looks up the value of the hidden input on the search form, and assigns it to the variable, ‘$search_refer’.

After that, the if statement decides, depending on the value of ‘$search_refer’, which template to load. The load_template function will ensure that the referenced page is shown, displaying the results from the search term.

One last thing you’ll need to do is edit the Post index. In this example, I’ll show you what was added to ‘blog-index.php’. This goes directly before The Loop.

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts("s=$s&amp;paged=$paged&amp;cat=4");
endif;

For now, I’m going to admit ignorance about the second line (and if you can explain it, then please, go ahead!). I don’t fully understand what purpose it serves — I pieced this solution together from a few places — but the third line (what’s with that?!) has the important bit you need to pay attention to.

On this client’s site, the ‘Blog’ Category has an ID number of ’4′; that’s why the ‘query_posts’ function has ‘cat=4′ at the end — this only includes Posts from that Category in the output.

On the ‘articles-index.php’ template page, ‘query_posts’ has ‘cat=3′, to only show ‘Articles’ Posts. Fairly straightforward, right?

In this particular example (and I thought it best to show it as closely to my working version as is clear), I’ve sent the different results to different template pages because those templates differ to a large degree. However, if your search results templates for the different sections are identical (or nearly), then you could skip out the ‘search.php’ file, and this to your index file:

if( is_search() ) :
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
if ($search_refer == 'blog') { query_posts("s=$s&amp;paged=$paged&amp;cat=4"); }
elseif ($search_refer == 'articles') { query_posts("s=$s&amp;paged=$paged&amp;cat=3"); };
endif;

That should do the whole lot after the search form in one step! If you’ve come up with a more elegant solution to this, or know a more efficient way to implement this solution, then please leave a comment — we all benefit from sharing tips like these!


21 Comments on Multiple Search Results Pages within One WordPress Site

  1. Great post dude! :)

    Bookmarked – coz I know this is gonna come up!

    Comment by Rob MacKay at 10:35 am, 14th May, 2009

  2. nice stuff i am looking for search result to index in search engine

    Comment by xinfo at 3:57 pm, 5th June, 2009

  3. Cheers guys, I hope it comes in handy for you both.

    Comment by Rob Barrett at 5:11 pm, 5th June, 2009

  4. Rob, this is awesome. Great piece of knowledge. Keep up the good work dude. I liked the way you explained this…

    Thanks!

    Comment by Glowtouch Technologies at 6:59 am, 19th June, 2009

  5. Thanks for getting in touch, I’m glad you found it easy to understand!

    Comment by Rob Barrett at 2:03 pm, 7th July, 2009

  6. Pingback: How to Customize Multiple Search Result Pages in Wordpress | Wordpress Garage

  7. Pingback: Disha Hosting » How to Customize Multiple Search Result Pages in Wordpress

  8. Great article, very clear. I’ve been looking for this all over…
    Keep up the good work !

    Comment by Mehdi at 8:11 pm, 10th August, 2009

  9. Seems like a great solution. Bookmarked, since like Rob, I also know it’s gonna come soon…
    Thanks

    Comment by Maor B. at 1:58 am, 1st September, 2009

  10. Wow…that’s great tips! i will try it,thanks rob!

    Comment by Jasa Pembuatan web at 3:57 pm, 8th November, 2009

  11. Nice tips, really helpful. thanks a lot!

    Comment by Hazel at 7:45 am, 18th January, 2010

  12. Hi,

    Congrats, it is a great tutorial and very useful. I can also understand it easily that I can also execute it because of your easy to follow instructions.

    Thanks,
    Rommel

    Comment by Furniture Lift at 2:31 pm, 28th February, 2010

  13. Thank for information. That’s good idea.

    Comment by sheath dress at 10:32 pm, 29th March, 2010

  14. It was a grateful moment while I’m reading your article. I have learned a lot from it.

    Comment by Limoges Porcelain Boxes at 12:54 am, 8th April, 2010

  15. It seems as a great tip. If I only know much in web design I would love to share something here. I think you are the only one have figure out this solution. Nice job.

    Comment by ohare airport limo service at 10:34 am, 15th April, 2010

  16. I cannot thank you enough. I have been looking to solve a search problem for about a month, and finally, with the help you give here, my different searches in different areas of the blog is working.
    Thank you so much. I already linked to your page in wordpress forum support because I am sure I am not the only one having this problem.
    Many thanks.

    Comment by Margarida Fernandes at 7:50 pm, 15th April, 2010

  17. Thank for function.

    Comment by trimmer at 8:46 pm, 20th April, 2010

  18. thank you!

    Comment by ben at 9:52 pm, 22nd April, 2010

  19. This is very timely for me. I’m been looking for this. This will be a great help for me. Thanks for sharing your knowledge. I wish you success in your business.

    Comment by Web design Darwin at 5:15 am, 11th June, 2010

  20. Hi Rob, many thanks for sharing this usefull information. I will try on my wordpress blog.

    Comment by baju import korea at 9:34 am, 27th July, 2010

  21. Great solution! Seems so simple, but there’s no way I could have come up with that from scratch… can you post a link to any of your original sources for what you pieced together, just so I can get a little context around where you even went to find this great stuff? That way I don’t have to just copy and paste your solution wholesale, I can do my own research a bit :-)

    Comment by HH Gregg at 6:56 pm, 6th January, 2011