0

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

Try adding the return as well in the else{}, like this:

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 locate_template('archive-products.php');
}
 return $template;
}
    add_filter('template_include', 'template_chooser');

Haven't tested.

  • I gave it a shot, no luck though. I appreciate the idea though. – jasenmp Sep 24 '14 at 17:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.