Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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>
@ZaheerAbbasAghani

This comment has been minimized.

Copy link

ZaheerAbbasAghani commented Jan 19, 2016

but how can i show form in frontend ???

@wattswing

This comment has been minimized.

Copy link

wattswing commented Feb 9, 2016

@ZaheerAghani :
You can use get_search_form() function.
From codex : it will render your searchform.php template file.

@ZaheerAbbasAghani

This comment has been minimized.

Copy link

ZaheerAbbasAghani commented Jan 7, 2017

I mean how can i display search results ?

@storiediweb

This comment has been minimized.

Copy link

storiediweb commented Jun 13, 2017

It's a clean solution and works perfectly, thank you!

@normanaranez

This comment has been minimized.

Copy link

normanaranez commented Feb 15, 2018

How to add it on the wordpress theme? am i going to add it on the function.php? how?

@YuvrajKhavad

This comment has been minimized.

Copy link

YuvrajKhavad commented Aug 22, 2018

Hello @galengidman

Your solutions working perfect thanks.

@junaidtk

This comment has been minimized.

Copy link

junaidtk commented Oct 1, 2019

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 ?

@galengidman

This comment has been minimized.

Copy link
Owner Author

galengidman commented Oct 1, 2019

@junaidtk Yes, it must be a ?s=[search query] GET request to the root of the site.

@junaidtk

This comment has been minimized.

Copy link

junaidtk commented Oct 3, 2019

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

404error

@galengidman

This comment has been minimized.

Copy link
Owner Author

galengidman commented Oct 3, 2019

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

@junaidtk

This comment has been minimized.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.