{"id":116,"date":"2020-01-28T09:32:19","date_gmt":"2020-01-28T09:32:19","guid":{"rendered":"http:\/\/chamankumar.co.uk\/blog\/?p=116"},"modified":"2020-01-28T09:32:19","modified_gmt":"2020-01-28T09:32:19","slug":"add-woocommerce-product-category-description-to-main-wordpress-search","status":"publish","type":"post","link":"https:\/\/chamankumar.co.uk\/blog\/wordpress\/add-woocommerce-product-category-description-to-main-wordpress-search","title":{"rendered":"Add woocommerce product category description to main wordpress search"},"content":{"rendered":"<p>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).<\/p>\n<p><code><\/p>\n<pre>\r\nadd_action( 'loop_start', function(){\r\n  $searchvar = get_search_query();\r\n  $paged = get_query_var('paged'); \r\n  $debug = 'search: '. $searchvar;\r\n  \/\/ only show on seach page and only for first page of results\r\n  if($searchvar>'' && $paged==0){\r\n\t$s = $searchvar;\r\n\t$results = array();\r\n\t$searchterms = explode(' ',$s); \/\/ \"+\" is removed!\r\n\t\/\/print_r($searchterms);\r\n\t$termcount=0;\r\n\tforeach($searchterms as $searchterm){\r\n\t\t$debug .= '<br><br>' . $searchterm . '<br>--------------------------';\r\n\t\t$termcount++;\r\n\t\t$exclude_arr = array();\r\n\t\t${'termsfound'.$termcount} = array($searchterm);\r\n\t\t\/\/ search product category title\r\n\t\t$debug .= '<br><br>category title';\r\n\t\t$terms = get_terms(array(\r\n\t\t\t'taxonomy' => 'product_cat',\r\n\t\t\t'name__like' => $searchterm,\r\n\t\t\t'hide_empty' => true, \/\/ Optional \r\n\t\t));\r\n\t\tif ( count($terms) > 0 ){\r\n\t\t\tforeach ( $terms as $term ) {\r\n\t\t\t\t$results[] = $term;\r\n\t\t\t\t$exclude_arr[] = $term->term_id;\r\n\t\t\t\t${'termsfound'.$termcount}[] = $term->term_taxonomy_id;\r\n\t\t\t}\r\n\t\t\t$debug .= '<br>excluded=' . implode(',',$exclude_arr);\r\n\t\t\t$debug .= '<br>terms=' . implode(',',${'termsfound'.$termcount});\r\n\t\t}\r\n\t\t\/\/ search product category description\r\n\t\t$debug .= '<br><br>category description';\r\n\t\t$exclude = implode(\",\", $exclude_arr);\r\n\t\t$terms2 = get_terms(array(\r\n\t\t\t'taxonomy' => 'product_cat',\r\n\t\t\t'description__like' => $searchterm,\r\n\t\t\t'hide_empty' => true, \/\/ Optional \r\n\t\t\t'exclude' => $exclude,\r\n\t\t));\r\n\t\tif ( count($terms2) > 0 ){\r\n\t\t\tforeach ( $terms2 as $term2 ) {\r\n\t\t\t\t$results[] = $term2;  \r\n\t\t\t\t$exclude_arr[] = $term2->term_id;\r\n\t\t\t\t${'termsfound'.$termcount}[] = $term2->term_taxonomy_id;\r\n\t\t\t}\r\n\t\t\t$debug .= '<br>excluded=' . implode(',',$exclude_arr);\r\n\t\t\t$debug .= '<br>terms2=' . implode(',',${'termsfound'.$termcount});\r\n\t\t}\r\n\t\t\/\/ search product category acf description2 field\r\n\t\t$debug .= '<br><br>acf description2 field';\r\n\t\t$exclude = implode(\",\", $exclude_arr);\r\n\t\t$description2terms = get_terms(array(\r\n\t\t\t'taxonomy' => 'product_cat',\r\n\t\t\t'hide_empty' => true, \r\n\t\t\t'exclude' => $exclude,\r\n\t\t\t'meta_query' => array(\r\n\t\t\t\tarray(\r\n\t\t\t\t'key'       => 'description_2',\r\n\t\t\t\t'value'     => $searchterm,\r\n\t\t\t\t'compare'   => 'LIKE'\r\n\t\t\t\t),\r\n\t\t\t),\r\n\t\t));\r\n\t\tif ( count($description2terms) > 0 ){\r\n\t\t\tforeach ( $description2terms as $term3 ) {\r\n\t\t\t\t$results[] = $term3;  \r\n\t\t\t\t${'termsfound'.$termcount}[] = $term3->term_taxonomy_id;\r\n\t\t\t}\r\n\t\t\t$debug .= '<br>terms3=' . implode(',',${'termsfound'.$termcount});\r\n\t\t}\r\n\t}\r\n\tif ($termcount>1) {\r\n\t\t\/\/ search is AND, therefore only show results which appear in all result sets\r\n\t\t$commonterms = $termsfound1;\r\n\t\tfor ($i=2; $i<=$termcount; $i++) {\r\n\t\t\t$commonterms = array_intersect($commonterms, ${'termsfound'.$i});\r\n\t\t}\r\n\t\t$commonterms = array_values($commonterms);\r\n\t\tif(count($commonterms) > 0){\r\n\t\t\t$debug .= '<br><br>common terms=' . implode(',',$commonterms);\r\n\t\t\t$terms3 = get_terms(array(\r\n\t\t\t\t'taxonomy' => 'product_cat',\r\n\t\t\t\t'include' => $commonterms,\r\n\t\t\t));\r\n\t\t\tforeach ( $terms3 as $term ) {\r\n\t\t\t\t$finalresults[] = $term;\r\n\t\t\t}\r\n\t\t} else {\r\n\t\t\t$finalresults = array(); \/\/ no common results\r\n\t\t\t\/\/echo 'NOTHING!';\r\n\t\t}\r\n\t} else {\r\n\t\t$finalresults = $results; \/\/ one one term searched for\r\n\t}\r\n\t\/\/echo '<pre>'; echo $debug; echo '<\/pre>\n<p>';<br \/>\n\t\/\/ combine results<br \/>\n\tif ( count($finalresults) > 0 ){<br \/>\n\t\t\/*  [term_id] [name] [slug] [term_group] [term_taxonomy_id] [taxonomy] [description] [parent] [count] [filter] [term_order] *\/<br \/>\n\t\tforeach ( $finalresults as $result ) {<br \/>\n\t\t\t$thumbnail_id = get_woocommerce_term_meta( $result->term_id, 'thumbnail_id', true );<br \/>\n\t\t\t$imageurl = wp_get_attachment_image_url( $thumbnail_id, 'swiper_news_image_size' );<br \/>\n\t\t\t$imageurlsm = wp_get_attachment_image_url( $thumbnail_id, 'post_image_thumb' );<br \/>\n\t\t\t\/\/$excerpt = esc_html( $result->description );<br \/>\n\t\t\t$excerpt = substr(esc_html( $result->description ),0,strpos(esc_html( $result->description ),' ',250));<br \/>\n\t\t\techo '<\/p>\n<article class=\"elementor-post elementor-grid-item post-2333 product type-product status-publish has-post-thumbnail product_cat-accessories first instock taxable shipping-taxable purchasable product-type-simple\">\n<div class=\"elementor-post__card\">\n\t\t\t\t<a class=\"elementor-post__thumbnail__link\" href=\"' . esc_url( get_term_link( $result ) ) . '\"><\/p>\n<div class=\"elementor-post__thumbnail elementor-fit-height\"><img src=\"' . $imageurl . '\" class=\"attachment-swiper_news_image_size size-swiper_news_image_size\" alt=\"' . esc_attr( $result->name ) . '\" srcset=\"' . $imageurl . ' 610w,   ' . $imageurlsm . ' 295w\" sizes=\"(max-width: 610px) 100vw, 610px\" width=\"610\" ><\/div>\n<p>\t\t<\/a><\/p>\n<div class=\"elementor-post__text\">\n<h3 class=\"elementor-post__title\">\n\t\t\t<a href=\"' . esc_url( get_term_link( $result ) ) . '\">' . esc_html( $result->name ) . '<\/a><br \/>\n\t\t<\/h3>\n<div class=\"elementor-post__excerpt\">\n<p>'. $excerpt .'<\/p>\n<\/p><\/div>\n<p>\t\t\t\t\t<a class=\"elementor-post__read-more\" href=\"' . esc_url( get_term_link( $result ) ) . '\"><br \/>\n\t\t\t\tRead More \u00bb\t\t\t<\/a>\n\t\t\t\t<\/div>\n<div class=\"elementor-post__meta-data\">\n\t\t\t\t\t<span class=\"elementor-post-date\"><\/span>\n\t\t\t\t<\/div>\n<\/p><\/div>\n<\/article>\n<p>';<br \/>\n\t\t}<br \/>\n\t}<br \/>\n  }<br \/>\n});<br \/>\n<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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( &#8216;loop_start&#8217;, function(){ $searchvar = get_search_query(); $paged = get_query_var(&#8216;paged&#8217;); $debug = &#8216;search: &#8216;. $searchvar; \/\/ only show on seach page and only for first page [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[10,21,8],"tags":[],"_links":{"self":[{"href":"https:\/\/chamankumar.co.uk\/blog\/wp-json\/wp\/v2\/posts\/116"}],"collection":[{"href":"https:\/\/chamankumar.co.uk\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/chamankumar.co.uk\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/chamankumar.co.uk\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/chamankumar.co.uk\/blog\/wp-json\/wp\/v2\/comments?post=116"}],"version-history":[{"count":1,"href":"https:\/\/chamankumar.co.uk\/blog\/wp-json\/wp\/v2\/posts\/116\/revisions"}],"predecessor-version":[{"id":117,"href":"https:\/\/chamankumar.co.uk\/blog\/wp-json\/wp\/v2\/posts\/116\/revisions\/117"}],"wp:attachment":[{"href":"https:\/\/chamankumar.co.uk\/blog\/wp-json\/wp\/v2\/media?parent=116"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/chamankumar.co.uk\/blog\/wp-json\/wp\/v2\/categories?post=116"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/chamankumar.co.uk\/blog\/wp-json\/wp\/v2\/tags?post=116"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}