Skip to content

Instantly share code, notes, and snippets.

@josephhinson
Last active December 28, 2015 10:29
Show Gist options
  • Select an option

  • Save josephhinson/7486564 to your computer and use it in GitHub Desktop.

Select an option

Save josephhinson/7486564 to your computer and use it in GitHub Desktop.
Search Filter (to return results by post type). Create a custom search form where you can set a hidden input to search that specific post type...see below the PHP block.
<?php
function otg_optional_search_filter($query) {
$post_type = $_GET['type'];
if (!$post_type) {
$post_type = 'any';
}
if ($query->is_search) {
$query->set('post_type', $post_type);
};
return $query;
};
add_filter('pre_get_posts','otg_optional_search_filter');
// Search form below allows you to pass a hidden input called "type" whose value will be parsed through the search function
?>
<form class="form-search" method="get" action="<?php bloginfo('url'); ?>" id="searchform">
<input type="text" placeholder="search the site" class="input-medium search-query" name="s">
<input type="hidden" name="type" value="post" id="type">
<button type="submit" class="btn">Search</button>
</form>
<?php
// You can additionally use more than one post type by setting the inputs up as arrays, seen below:
?>
<form class="form-search" method="get" action="<?php bloginfo('url'); ?>" id="searchform">
<input type="text" placeholder="search the site" class="input-medium search-query" name="s">
<input type="hidden" name="type[]" value="page" id="type">
<input type="hidden" name="type[]" value="post" id="type">
<button type="submit" class="btn">Search</button>
</form>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment