WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.


share|improve this question

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


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.