Skip to content

Instantly share code, notes, and snippets.

@Olaw2jr
Forked from galengidman/search.php
Created January 12, 2018 07:42
Show Gist options
  • Save Olaw2jr/6c50d1356f46650282684c223db8a139 to your computer and use it in GitHub Desktop.
Save Olaw2jr/6c50d1356f46650282684c223db8a139 to your computer and use it in GitHub Desktop.
WordPress search form & results for custom post type
<?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;
}
?>
<!-- default search results here -->
<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="kb_article">
</form>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment