Last active
September 26, 2021 16:45
-
-
Save galengidman/8b84770a2dcc9abb8bfe to your computer and use it in GitHub Desktop.
WordPress search form & results for custom post type
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// check to see if there is a post type in the URL | |
if ( isset( $_GET['post_type'] ) && $_GET['post_type'] ) { | |
// save it for later | |
$post_type = $_GET['post_type']; | |
// check to see if a search template exists | |
if ( locate_template( 'search-' . $post_type . '.php' ) ) { | |
// load it and exit | |
get_template_part( 'search', $post_type ); | |
exit; | |
} | |
} | |
?> | |
<!-- default search results here --> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<form class="search" action="<?php echo home_url( '/' ); ?>"> | |
<input type="search" name="s" placeholder="Search…"> | |
<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
Thanks @galengidman, its work for me!