0

So I have a section on my site that specifically searches a custom post type for YouTube videos. I'm absolutely able to search the custom type. However, I'm unsure how to create a search page that has custom formatting geared toward this type of search.

I've created a custom loop-youtube.php and modified within search.php get_template_part( 'loop'); to get_template_part( 'loop', 'youtube'); however, it affects the search results globally.

Is there a way to create a custom search.php page for a specific post_type?

Thanks for any help.

2 Answers 2

2

How are you restricting the search to your custom post type? If you are doing it by passing an additional argument, i.e. &type=myCustomPostType, you could use a conditional test, like:

if(isset($_GET['type'] && $_GET['type'] == 'myCustomPostType')):
  get_template_part('loop','youtube');
else:
 get_template_part(loop);
endif;
4
  • I added a search filter in functions.php then I added a hidden query to the form <input type="hidden" name="post_type" value="youtube" /> to the form. Mar 3, 2011 at 21:56
  • Then change what i did above to only test for $_GET['post_type'] == 'youtube' and i think that should do it. Now, I'm also assuming that you're either using a dedicated form just for searching youtube post types or doing some kind of test before applying the search filter. If not that filter will also affect search results globally.
    – starepod
    Mar 4, 2011 at 0:46
  • I seem to be getting a syntax error on the first line..I fixed the semi colon at the end..still getting the same error. if(isset($_GET['type'] && $_GET['post_type'] == 'youtube')); Mar 4, 2011 at 15:06
  • if(isset($_GET['type']) && $_GET['post_type'] == 'youtube'): I didn't close the isset function. The colon should be a colon, not a semicolon. It works just like the brackets if{ } else { } do.
    – starepod
    Mar 4, 2011 at 23:24
1

I have my search page split up like this:

<?php if ($wp_query->query_vars['post_type'] == 'post') { 
 // output for posts format
} else {
 // output for other formats
} ?>

I think query_vars is more efficient than $_GET in this case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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