jcadima
7/10/2017 - 1:15 PM

Search form for custom post type

Search form for custom post type

https://wordpress.stackexchange.com/questions/248983/using-standard-search-function-with-custom-post-type
https://gist.github.com/galengidman/8b84770a2dcc9abb8bfe


1) THE HTML THAT DISPLAYS THE FORM with the custom post type as hidden value
this will diplay results of the custom post type only , it wont match
any other keywords that might exist in other pages
this is only needed for exclusive custom post type search only
<form class="search" action="<?php echo home_url( '/' ); ?>">
  <input type="search" name="s" placeholder="Search&hellip;">
  <input type="submit" value="Search">
  <input type="hidden" name="post_type" value="your_post_type_here">
</form>


// THIS IS OPTIONAL:
2) Create search-your_custom_post_type_here.php
Add the following:

<?php
// store the post type from the URL string
$post_type = $_GET['post_type'];
// check to see if there was a post type in the
// URL string and if a results template for that
// post type actually exists
if ( isset( $post_type ) && locate_template( 'search-' . $post_type . '.php' ) ) {
  // if so, load that template
  get_template_part( 'search', $post_type );
  
  // and then exit out
  exit;
}