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.


2 Answers 2


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


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>

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;?>

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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