search only pages if on page

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

Leave a Comment