Forums › Forums › Web Design › WordPress › Modify search query to include taxonomies
-
This is a function to include specific custom taxonomies into the search query for WordPress. It works alongside searching for title and everything else because it uses an OR statement instead of the original AND statement. function _pd_search_taxonomy( $clauses, $query ) { // IGNORE ADMIN REQUESTS if ( is_admin() ) { return $clauses; } global $wpdb; if($query->query_vars){ // GET THE SEARCH TERM $searchterm = $query->query_vars[‘s’]; } $clauses[‘join’] .= term_relationships}.object_id LEFT OUTER JOIN {$wpdb->term_taxonomy} USING (term_taxonomy_id) LEFT OUTER JOIN {$wpdb->terms} USING (term_id) SQL; // REPLACE taxonomy = WITH YOUR OWN TAXONOMY SLUG $clauses[‘where’] .= ” OR (taxonomy = ‘sub_search_terms’ AND taxonomy IS NOT NULL AND $wpdb->terms.slug = ‘” . $searchterm . “‘)”; return $clauses; } add_filter( ‘posts_clauses’, ‘_pd_search_taxonomy’, 10, 2 ); – by hq overview pixeldevs – –