Skip to content

Instantly share code, notes, and snippets.

@jdevalk
Created November 8, 2011 11:50
Show Gist options
  • Save jdevalk/1347575 to your computer and use it in GitHub Desktop.
Save jdevalk/1347575 to your computer and use it in GitHub Desktop.
Function to move searches for non-post post-types / taxonomies to their respective archives.
<?php
function yoast_change_template( $template ) {
global $wp_query;
if ( !isset($wp_query->query_vars['taxonomy']) )
return $template;
$taxonomy = get_taxonomy( $wp_query->query_vars['taxonomy'] );
if ( is_search() && count( $taxonomy->object_type ) > 0 && !in_array( 'post', array_values($taxonomy->object_type) ) ) {
set_query_var('post_type', $taxonomy->object_type[0]);
$newtemplate = get_archive_template();
if ( $newtemplate )
$template = $newtemplate;
}
return $template;
}
add_filter('template_include','yoast_change_template',10,1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment