Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

i have a site made up of pages and posts. there are only a few pages and a ton of posts. how can i set the search box in my sidebars to search pages only if on a page, or search posts only if on my blog page? right now it mixes everything together on my search page.

it would be even cooler if there was a way to add checkboxes under the search box to select posts or pages.

thanks!

share|improve this question

2 Answers 2

You can append post_type to the end of your search string like this:

http://yourdomain.com/?s=search+string&post_type=page

This works like a charm:

<form method="get" action="<?php echo home_url();?>">
    <input type="text" name="s" />
    <label>Search Pages</label>
    <input type="radio" name="post_type" value="page" />
    <label>Search Posts</label>
    <input type="radio" name="post_type" value="post" />
    <button type="submit">Submit</button>
</form>

You could also do something like this within the form tag (sans the radio buttons) for contextual searching:

<?php if(is_page()):?>
    <input type="hidden" name="post_type" value="page" />
<?php endif;?>
share|improve this answer

Conditional tags: is_page() or is_archive(). There's also is_singular(), but you better take a look at this in Codex.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.