Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jfeliweb/30dbd022851a834570ea to your computer and use it in GitHub Desktop.
Save jfeliweb/30dbd022851a834570ea to your computer and use it in GitHub Desktop.
Create multiple Search functions for posts & custom post types and everything
/* Search Everything*/
function SearchFilter($query) {
$post_type = $_GET['post_type'];
if (!$post_type) {
$post_type = 'any';
}
if ($query->is_search) {
$query->set('post_type', $post_type);
};
return $query;
}
add_filter('pre_get_posts','SearchFilter');
/* Display Search Field */
<form class="headerForm" role="search" action="<?php bloginfo( 'url' ); ?>/" method="get" id="searchform">
<input type="text" name="s" class="s" id="s" value="" placeholder="Search"/>
<button><?php _e("Search");?></button>
</form>
/* news search form add */
<input type="hidden" name="post_type" value="post" />
/* custom post type listing search form add */
<input type="hidden" name="post_type" value="customtypename" />
/* entire site search don't add anything for value="" */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment