The only way I could find to do this was to search for the terms separately and then insert the results before the main result (only on page 1 though).
add_action( 'loop_start', function(){
$searchvar = get_search_query();
$paged = get_query_var('paged');
$debug = 'search: '. $searchvar;
// only show on seach page and only for first page of results
if($searchvar>'' && $paged==0){
$s = $searchvar;
$results = array();
$searchterms = explode(' ',$s); // "+" is removed!
//print_r($searchterms);
$termcount=0;
foreach($searchterms as $searchterm){
$debug .= '
' . $searchterm . '
--------------------------';
$termcount++;
$exclude_arr = array();
${'termsfound'.$termcount} = array($searchterm);
// search product category title
$debug .= '
category title';
$terms = get_terms(array(
'taxonomy' => 'product_cat',
'name__like' => $searchterm,
'hide_empty' => true, // Optional
));
if ( count($terms) > 0 ){
foreach ( $terms as $term ) {
$results[] = $term;
$exclude_arr[] = $term->term_id;
${'termsfound'.$termcount}[] = $term->term_taxonomy_id;
}
$debug .= '
excluded=' . implode(',',$exclude_arr);
$debug .= '
terms=' . implode(',',${'termsfound'.$termcount});
}
// search product category description
$debug .= '
category description';
$exclude = implode(",", $exclude_arr);
$terms2 = get_terms(array(
'taxonomy' => 'product_cat',
'description__like' => $searchterm,
'hide_empty' => true, // Optional
'exclude' => $exclude,
));
if ( count($terms2) > 0 ){
foreach ( $terms2 as $term2 ) {
$results[] = $term2;
$exclude_arr[] = $term2->term_id;
${'termsfound'.$termcount}[] = $term2->term_taxonomy_id;
}
$debug .= '
excluded=' . implode(',',$exclude_arr);
$debug .= '
terms2=' . implode(',',${'termsfound'.$termcount});
}
// search product category acf description2 field
$debug .= '
acf description2 field';
$exclude = implode(",", $exclude_arr);
$description2terms = get_terms(array(
'taxonomy' => 'product_cat',
'hide_empty' => true,
'exclude' => $exclude,
'meta_query' => array(
array(
'key' => 'description_2',
'value' => $searchterm,
'compare' => 'LIKE'
),
),
));
if ( count($description2terms) > 0 ){
foreach ( $description2terms as $term3 ) {
$results[] = $term3;
${'termsfound'.$termcount}[] = $term3->term_taxonomy_id;
}
$debug .= '
terms3=' . implode(',',${'termsfound'.$termcount});
}
}
if ($termcount>1) {
// search is AND, therefore only show results which appear in all result sets
$commonterms = $termsfound1;
for ($i=2; $i<=$termcount; $i++) {
$commonterms = array_intersect($commonterms, ${'termsfound'.$i});
}
$commonterms = array_values($commonterms);
if(count($commonterms) > 0){
$debug .= '
common terms=' . implode(',',$commonterms);
$terms3 = get_terms(array(
'taxonomy' => 'product_cat',
'include' => $commonterms,
));
foreach ( $terms3 as $term ) {
$finalresults[] = $term;
}
} else {
$finalresults = array(); // no common results
//echo 'NOTHING!';
}
} else {
$finalresults = $results; // one one term searched for
}
//echo ''; echo $debug; echo '
';
// combine results
if ( count($finalresults) > 0 ){
/* [term_id] [name] [slug] [term_group] [term_taxonomy_id] [taxonomy] [description] [parent] [count] [filter] [term_order] */
foreach ( $finalresults as $result ) {
$thumbnail_id = get_woocommerce_term_meta( $result->term_id, 'thumbnail_id', true );
$imageurl = wp_get_attachment_image_url( $thumbnail_id, 'swiper_news_image_size' );
$imageurlsm = wp_get_attachment_image_url( $thumbnail_id, 'post_image_thumb' );
//$excerpt = esc_html( $result->description );
$excerpt = substr(esc_html( $result->description ),0,strpos(esc_html( $result->description ),' ',250));
echo '
';
}
}
}
});