Multiple Search Results Pages within One WordPress Site

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 method="get" id="searchform" action="<?php bloginfo('home'); ?>/">
<input type="text" name="s" id="s" value="Search" size="18" maxlength="96" onblur="if(this.value=='') this.value='Search';" onfocus="if(this.value=='Search') this.value='';" />
<input type="hidden" name="site_section" value="blog" />
<input type="submit" value="Search" class="hidden" id="searchsubmit" />
</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:

<?php
/* 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.

<?php if( is_search() ) :
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts("s=$s&paged=$paged&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:

<?php $search_refer = $_GET["site_section"];
if( is_search() ) :
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
if ($search_refer == 'blog') { query_posts("s=$s&paged=$paged&cat=4"); }
elseif ($search_refer == 'articles') { query_posts("s=$s&paged=$paged&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!

Quick WordPress Tip: How to execute AND display PHP in posts and pages

Say you’ve got some PHP code that you need to run from within one of your Pages. For example, in my Portfolio page, I use the FlickrRSS plugin to pull in photos from my Flickr feed. While the easiest way to implement that would be to place the required PHP snippet in a template file, [...]

Read the rest of this entry »

Quick WordPress Tip: Style Posts According to Date

Ever wanted to give a post element a different look and feel (or even include/exclude content) based on a post’s publication date? I got asked about this earlier today, and I figured it would be a handy tip to share.
I don’t claim to be a PHP coder, so while it does work, it could be [...]

Read the rest of this entry »

6 Steps to the Perfect Cup of Tea

Title image for 6 Steps to the Perfect Cup of Tea

Thanks to my friend Anca Foster, I was up until 3am this morning discussing my ideal method for making a cup of English Breakfast tea, so in between explaining my method and theory behind it to her over Twitter, I sketched out this informative panel to help all of you wannabes …and those weirdos out [...]

Read the rest of this entry »

Tutorial: Twitterlove Bird, from sketch to vector in Photoshop and Illustrator

Title image for Tutorial: Twitterlove Bird, from sketch to vector in Photoshop and Illustrator

Following the recent launch of SoulTweet, Graham Smith made a call out for any illustrators to help out with the site.
In a few minutes that I had spare, I doodled a couple of birds to offer up for use, and sent across the best of the two for consideration. It didn’t get taken up, so [...]

Read the rest of this entry »

Rob Barrett Introduction