Below I have a function working that includes my custom posts in search results. I would like to now display these results in a specific template. I tried adding the two commented out lines but it's not finding the template-stories.php file. What am I doing wrong? Thanks

function filter_search($query) {
    if ($query->is_search) {
       $query->set('post_type', array('post', 'story', 'videos'));
     //return locate_template('template-stories.php');
return $query;
add_filter('pre_get_posts', 'filter_search', 11);
//add_filter('template_include', 'filter_search', 10);

Update: I tried a modification of GM's answer from this question but it didn't work. I can get the correct results with $query->set and pre_get_posts above, but can't get the display in the template. Meanwhile I can get the display but not the correct results with the template_include function (below) leaving out the pre_get_posts. Anyone know how to combine both these functions:

function template_chooser($template) {    
  global $wp_query;   
  $post_type = get_query_var('post_type');

  if( $wp_query->is_search && $post_type == array('story', 'videos', 'post') )  
    return locate_template('template-stories.php'); 
  return $template;   
add_filter('template_include', 'template_chooser'); 
  • Since you're already setting the 3 post types and you want to display the same template-stories.php on all 3 templates ( according to your current code ) wouldn't it be easier to just edit search.php to use your templates structure? Or do you only want specific styles to display whenever post type story pops up in your search? – Howdy_McGee Nov 7 '14 at 15:23
  • I need the 3 post types search results to display seamlessly on the same page in a very specific style (carousel) under that pages content. – amespower Nov 7 '14 at 15:29
  • I mean it sounds like you want each post type to have a specific style which is probably best done during the loop and testing the post type as you go. – Howdy_McGee Nov 7 '14 at 15:34

Your Answer

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

Browse other questions tagged or ask your own question.