I’m using elementor and I’d like to add taxonomy term name in the search form.
eg there’s a taxonomy called “features” with the slug “listing_features” and one of the terms is “gym”.
I want to type “gym in the search form and return posts that include gym as a taxonomy.
chatgpt gave me this as a solution but it doesnt seem to work…
function extend_search_to_include_taxonomies( $query ) {
if ( $query->is_search && !is_admin() ) {
$query->set( 'tax_query', array(
'relation' => 'OR',
array(
'taxonomy' => 'category',
'field' => 'name',
'terms' => $query->query_vars['s'],
'operator' => 'LIKE'
),
array(
'taxonomy' => 'post_tag',
'field' => 'name',
'terms' => $query->query_vars['s'],
'operator' => 'LIKE'
)
));
}
}
add_action( 'pre_get_posts', 'extend_search_to_include_taxonomies' );