I've built a CPT that has three different custom taxonomies, I've also built a custom search form that has fields for each taxonomy, and a field for "search by keyword".
I want the taxonomy terms associated with each instance of the CPT to be included in the "search by keyword" field. By default, WordPress does not include custom taxonomy in its search however I need to find a way to make this happen. I would prefer to not use a plugin to accomplish this.
What I am thinking of is injecting all the custom taxonomy terms associated with each CPT into the default content area, the default content area is not being used anyway, each CPT is put together with ACF, so it's currently blank and I am free to use it in this way.
MY QUESTION: Is there a filter or way I can inject all of the custom taxonomy terms into the content field when a CPT is published or updated?
Below is the code that I am using to create the query for context. This works fine, I just need the default search behavior to look in the custom taxonomies...
As an example is a user types "hospital" in the industry_select
field it will include all the CPT's that have the word "hospital" in their industry taxonomy terms.
However, if they were to type "hospital" in the keyword
field nothing will show up because nothing in the content or title has the word hospital in it...
function custom_search( $query ) {
if ( is_post_type_archive( 'experts' ) && $query->is_main_query() && !is_admin() ) {
$keyword = get_query_var( 'keyword', FALSE );
$industry_select = strtolower(str_replace(' ', '-', get_query_var( 'industry_select', FALSE ) ));
$speciality_select = strtolower(str_replace(' ', '-', get_query_var( 'speciality', FALSE ) ));
$location_select = strtolower(str_replace(' ', '-', get_query_var( 'location_select', FALSE ) ));
// create list of terms similar to what the user typed in
$broad_match_ind = get_terms( array(
'taxonomy' => 'experts_industries',
'fields' => 'slugs',
'name__like' => $industry_select,
'hide_empty' => false
) );
$broad_match_spec = get_terms( array(
'taxonomy' => 'experts_specialities',
'fields' => 'slugs',
'name__like' => $speciality_select,
'hide_empty' => false
) );
$broad_match_loc = get_terms( array(
'taxonomy' => 'experts_locations',
'fields' => 'slugs',
'name__like' => $location_select,
'hide_empty' => false
) );
// Keywords query
$keyword ? $keyword : $keyword = null;
$query->set('s', $keyword);
// Build taxonomy array based on what's been filled out
$tax_query_array = array('relation' => 'OR');
$location_select ? array_push($tax_query_array, array('taxonomy' => 'experts_locations', 'field' => 'slug', 'terms' => $broad_match_loc) ) : null ;
$speciality_select ? array_push($tax_query_array, array('taxonomy' => 'experts_specialities', 'field' => 'slug', 'terms' => $broad_match_spec) ) : null ;
$industry_select ? array_push($tax_query_array, array('taxonomy' => 'experts_industries', 'field' => 'slug', 'terms' => $broad_match_ind) ) : null ;
$query->set( 'tax_query', $tax_query_array);
}
}
add_action( 'pre_get_posts', 'custom_search' );