Skip to content

Instantly share code, notes, and snippets.

@galengidman
Last active September 26, 2021 16:45
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save galengidman/8b84770a2dcc9abb8bfe to your computer and use it in GitHub Desktop.
Save galengidman/8b84770a2dcc9abb8bfe to your computer and use it in GitHub Desktop.
WordPress search form & results for custom post type
<?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 -->
<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>
@galengidman
Copy link
Author

@junaidtk That's probably the issue. I would see if I could change name value on the product field.

@junaidtk
Copy link

junaidtk commented Oct 8, 2019

@galengidman, Thanks for the update. We can add the additional fields in add to cart form of product page by using the hooks available in the add to cart form. Also I am still checking the ways to solve this.

@suresh-dunga
Copy link

Thanks @galengidman, its work for me!

@foxnove
Copy link

foxnove commented Sep 17, 2021

Hi, @galengidman. How can I add the search for categories or taxonomies to this form?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment