Skip to content

Instantly share code, notes, and snippets.

@bueltge
Created June 9, 2011 11:58
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save bueltge/1016587 to your computer and use it in GitHub Desktop.
Save bueltge/1016587 to your computer and use it in GitHub Desktop.
Include custom taxonomy term in WordPress search
via http://wordpress.stackexchange.com/questions/2623/include-custom-taxonomy-term-in-search/5404#5404
function atom_search_where($where){
global $wpdb;
if ( is_search() )
$where .= "OR (t.name LIKE '%".get_search_query() . "%' AND {$wpdb->posts} . post_status = 'publish')";
return $where;
}
function atom_search_join($join){
global $wpdb;
if ( is_search() )
$join .= "LEFT JOIN {$wpdb->term_relationships} tr ON {$wpdb->posts}.ID = tr.object_id INNER JOIN {$wpdb->term_taxonomy} tt ON tt.term_taxonomy_id=tr.term_taxonomy_id INNER JOIN {$wpdb->terms} t ON t.term_id = tt.term_id";
return $join;
}
function atom_search_groupby($groupby){
global $wpdb;
// we need to group on post ID
$groupby_id = "{$wpdb->posts} . ID";
if ( ! is_search() || strpos($groupby, $groupby_id) !== false )
return $groupby;
// groupby was empty, use ours
if ( ! strlen( trim($groupby) ) )
return $groupby_id;
// wasn't empty, append ours
return $groupby . ", " . $groupby_id;
}
add_filter('posts_where', 'atom_search_where');
@emamut
Copy link

emamut commented Jul 13, 2015

You missed

add_filter('posts_join', 'atom_search_join');
add_filter('posts_groupby', 'atom_search_groupby');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment