Custom Search Template for Custom Post Types

Question

I know this is asked a million times, and I’ve found the solution. It seems fairly straight forward but it’s not working for some reason. I wonder if it has something to do with the fact that I’m performing the search using this plugin: https://wordpress.org/plugins/search-filter/ but I have my doubts. I was hoping someone had some insight, maybe I’m missing something. For reference here is my code:

function template_chooser($template)
{
   global $wp_query;
   $post_type = get_query_var('post_type');
   if( $wp_query->is_search && $post_type == 'product' )
{
  return locate_template('archive-products.php');
} else {
   $args = array_merge( $wp_query->query, array( 'post_type' => 'post' ) );
   query_posts( $args ); 
}
 return $template;
}
    add_filter('template_include', 'template_chooser');

I’m using this with WooCommerce, the search and filter plugin is to select products by category, attribute, and by name. The search does work it just does not pull in the custom template. What I’m trying to do is create a custom results page to display the products in the same way that they are displayed on a WooCommerce category page. For some reason if you do a search without selecting anything it’ll use the standard results template that’s used for post, pages, etc. and not the WooCommerce product template. If you select a category or attribute it’ll display the WooCommerce product template as it should with search results.

Any help would be very much appreciated. Thank you.

0
, , , jasenmp 7 years 2014-09-23T09:21:52-05:00 0 Answers 79 views 0

Leave an answer

Browse
Browse