Search Terms – Querying on either description__like OR name__like in the same Term Query?

I don’t see any options for WordPress to search terms by both of name & description. So i combined 2 queries like @StephanieQ but maybe more simple way, checkout my ajax response below:

public function ajax_project_terms_search() {
    $results = array();
    $search_query = sanitize_text_field($_GET['q']);
    $withname = intval($_GET['withname']);
    $search_keys = array('name__like', 'description__like');
    $exclude = array();

    foreach ($search_keys as $search_key) {
        $terms = get_terms( array(
            'taxonomy' => 'project',
            'hide_empty' => false,
            'number' => 8,
            'exclude' => $exclude,
            $search_key => $search_query,
        ));

        // create results
        foreach ($terms as $term) {
            $exclude[] = $term->term_id;
            $results[] = array(
                'id' => isset($_GET['return_id']) ? $term->term_id : $term->slug,
                'text' => $withname ? $term->name . ' - ' . $term->description : $term->name,
            );
        }
    }

    wp_send_json($results);
    wp_die();
}

Leave a Comment