WordPress custom post type search results page

I set up a custom search form to search and display results of my custom post type. The form is:

<form class="search-form" action="<?php echo home_url( '/' ); ?>">
  <input type="search" name="s" placeholder="Search&hellip;">
  <input type="hidden" name="post_type" value="resource-library">  
  <input type="submit" value="Search">
</form>

In my search.php I want to direct folks to a custom search php file called search-resource-library.php if the content type is resource-library. So I put this at the top of my search.php page:

Read More
<?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;
}
?>

I get exactly what this is doing but I can’t seem to store the post type from the URL string supposedly set in the search form html.

Any ideas?

Related posts

1 comment

  1. I found the issue. My original code and method is correct. The url parameters were not being passed from the search form because a plugin called “Nice Search” was installed. This plugin redirects ?s=query searches to /search/query, and converts %20 to +.

Comments are closed.