-
-
Save galengidman/8b84770a2dcc9abb8bfe to your computer and use it in GitHub Desktop.
<?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…"> | |
<input type="submit" value="Search"> | |
<input type="hidden" name="post_type" value="kb_article"> | |
</form> |
It's a clean solution and works perfectly, thank you!
How to add it on the wordpress theme? am i going to add it on the function.php? how?
Hello @galengidman
Your solutions working perfect thanks.
How the Wordpress search form submission works. What is the method in the form tag of the search form ? is it must be get method ?
@junaidtk Yes, it must be a ?s=[search query]
GET
request to the root of the site.
I am using the woo-commerce plugin. On my product page, I am using the additional product field with name S. so if we are submitting the add to cart form, it goes to the 404 pages. I hope it is submitting the search form due to the name of the field S.
If we change the field name to any other value, it is working fine and doesn't go to the 404.
The attached is the screenshot after clicking the add to cart button while placing the name as S
@junaidtk That's probably the issue. I would see if I could change name value on the product field.
@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.
Thanks @galengidman, its work for me!
Hi, @galengidman. How can I add the search for categories or taxonomies to this form?
I mean how can i display search results ?