1

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' );

1 Answer 1

0

Even though extending your custom search would be the proper WP way to solve your problem, the answer to your workaround question would be the below function:

function change_content_on_save($post_id, $post, $update) {
  if ($_SERVER['REQUEST_METHOD'] !== 'POST' || empty($_POST))
    return;
  if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
    return;
  if (!current_user_can('edit_post', $post_id))
    return;
  
  // create your terms list and insert to content here:
  $post->post_content = $terms_list;
  
  // Delete hook to avoid endless loop
  remove_action('save_post', 'change_content_on_save', 10);
  wp_update_post($post);
}
add_action( 'save_post', 'change_content_on_save', 10, 3 );

save_post is an action triggered whenever a post or page is created or updated https://developer.wordpress.org/reference/hooks/save_post/

1
  • This pushed me in the right direction! Thanks. Also if you know of any resources off the top of your head for extending the WP search I would love to comb through that. I intend to update this in the future, but this will do for now! Commented Apr 21, 2022 at 16:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.