Created
February 3, 2017 19:18
-
-
Save zorzv/10adf724632688aa5c4e57fc7e473321 to your computer and use it in GitHub Desktop.
Include Taxonomy in Search
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Custom Search on Homepage | |
function custom_search_where($where){ | |
global $wpdb; | |
if (is_search() && get_search_query()) | |
$where .= "OR ((t.name LIKE '%".get_search_query()."%' OR t.slug LIKE '%".get_search_query()."%') AND {$wpdb->posts}.post_status = 'publish')"; | |
return $where; | |
} | |
function custom_search_join($join){ | |
global $wpdb; | |
if (is_search() && get_search_query()) | |
$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 custom_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 || !get_search_query()) 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','custom_search_where'); | |
add_filter('posts_join', 'custom_search_join'); | |
add_filter('posts_groupby', 'custom_search_groupby'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment