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

commented Jan 19, 2016

but how can i show form in frontend ???

@wattswing

This comment has been minimized.

Copy link

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

commented Jan 7, 2017

I mean how can i display search results ?

@storiediweb

This comment has been minimized.

Copy link

commented Jun 13, 2017

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

@normanaranez

This comment has been minimized.

Copy link

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

commented Aug 22, 2018

Hello @galengidman

Your solutions working perfect thanks.

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.