I've searched a good deal for some direction on this. I'm unsure if my title is using the appropriate diction (which I believe is why I'm having trouble finding a solution), so I'll state my problem and maybe someone can better see what I'm looking for :)
I have a WooCommerce category 'cat-x'
When I search using the WooCommerce search for 'cat-x,' some products with 'cat-x' in their description appear, but I would like to include all items from 'cat-x' in the results. I suppose what I am trying to say is: I would like to include category names in the search's query.
Any help would be appreciated. Thank you :)
Based on this article: (http://jamescollings.co.uk/blog/extending-woocommerce-search-query-include-custom-fields/), Here is some code I've tried, but it actually provided no results after querying:
<?php
/**
* Add sku, author, publisher and format to product search
*/
// hook into wp pre_get_posts
add_action('pre_get_posts', 'mc_woo_search_pre_get_posts');
/**
* Add custom join and where statements to product search query
* @param mixed $q query object
* @return void
*/
function mc_woo_search_pre_get_posts($q){
if ( is_search() ) {
add_filter( 'posts_join', 'mc_search_post_join' );
add_filter( 'posts_where', 'mc_search_post_excerpt' );
}
}
/**
* Add Custom Join Code for wp_mostmeta table
* @param string $join
* @return string
*/
function mc_search_post_join($join = ''){
global $wp_the_query;
// escape if not woocommerce searcg query
if ( empty( $wp_the_query->query_vars['wc_query'] ) || empty( $wp_the_query->query_vars['s'] ) )
return $join;
$join .= "INNER JOIN wp_postmeta AS mcmt1 ON (wp_posts.ID = mcmt1.post_id)";
return $join;
}
/**
* Add custom where statement to product search query
* @param string $where
* @return string
*/
function mc_search_post_excerpt($where = ''){
global $wp_the_query;
// escape if not woocommerce search query
if ( empty( $wp_the_query->query_vars['wc_query'] ) || empty( $wp_the_query->query_vars['s'] ) )
return $where;
$where = preg_replace("/post_title LIKE ('%[^%]+%')/", "post_title LIKE $1)
OR (mcmt1.meta_key = 'product_cat' AND CAST(mcmt1.meta_value AS CHAR) LIKE $1)
OR (mcmt1.meta_key = '_format' AND CAST(mcmt1.meta_value AS CHAR) LIKE $1 ", $where);
return $where;
}
?>