Skip to content

Instantly share code, notes, and snippets.

@marco-s
Forked from jdevalk/gist:1347575
Created March 21, 2012 19:33
Show Gist options
  • Save marco-s/2151785 to your computer and use it in GitHub Desktop.
Save marco-s/2151785 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