Ben*_*HER 4 php wordpress search custom-taxonomy woocommerce
我想要的是:修改 WooCommerce 搜索表单(在前端)的查询以通过搜索产品的名称、描述和产品标签来显示产品。
我所拥有的:我正在尝试使用受此答案启发的代码,该代码返回产品名称和描述的结果。但是,如果我使用标签名称进行搜索,则没有结果。搜索查询不搜索产品的标签。
如何重现此问题(将以下代码放在活动主题的 functions.php 文件中):
function search_product_by_tag( $search, &$query_vars ) {
global $wpdb, $pagenow;
if ( 'edit.php' == $pagenow || empty($search) ) {
return $search;
}
$args = array(
'posts_per_page' => -1,
'post_type' => 'product',
'meta_query' => array(
array(
'key' => 'taxonomy',
'value' => 'product_tag',
'field' => 'name',
'terms' => array($query_vars->query['s']),
'compare' => 'LIKE',
)));
$posts = get_posts( $args );
if ( empty( $posts ) ) return $search;
$get_post_ids = array();
foreach($posts as $post){
$get_post_ids[] = $post->ID;
}
if ( sizeof( $get_post_ids ) > 0 ) {
$search = str_replace( 'AND (((', "AND ((({$wpdb->posts}.ID IN (" . implode( ',', $get_post_ids ) . ")) OR (", $search);
}
return $search;
}
add_filter( 'posts_search', 'search_product_by_tag', 999, 2 );
Run Code Online (Sandbox Code Playgroud)
示例:我有一种产品:带有产品标签“糖果”的黑巧克力。使用此代码,如果我在搜索表单中搜索“巧克力”,产品将被返回。但如果我搜索“甜点”:没有结果。

您的代码中有很多错误和错误(例如需要税务查询)。
要仅在 WooCommerce 产品搜索 (前端)中包含WooCommmerce 产品标签术语,请使用以下内容:
add_filter( 'posts_search', 'woocommerce_search_product_tag_extended', 999, 2 );
function woocommerce_search_product_tag_extended( $search, $query ) {
global $wpdb, $wp;
$qvars = $wp->query_vars;
if ( is_admin() || empty($search) || ! ( isset($qvars['s'])
&& isset($qvars['post_type']) && ! empty($qvars['s'])
&& $qvars['post_type'] === 'product' ) ) {
return $search;
}
// Here set your custom taxonomy
$taxonomy = 'product_tag'; // WooCommerce product tag
// Get the product Ids
$ids = get_posts( array(
'posts_per_page' => -1,
'post_type' => 'product',
'post_status' => 'publish',
'fields' => 'ids',
'tax_query' => array( array(
'taxonomy' => $taxonomy,
'field' => 'name',
'terms' => esc_attr($qvars['s']),
)),
));
if ( count( $ids ) > 0 ) {
$search = str_replace( 'AND (((', "AND ((({$wpdb->posts}.ID IN (" . implode( ',', $ids ) . ")) OR (", $search);
}
return $search;
}
Run Code Online (Sandbox Code Playgroud)
代码位于活动子主题(或活动主题)的 functions.php 文件中。测试和工作。
为了让它在 WordPress 搜索上工作也替换:
Run Code Online (Sandbox Code Playgroud)if ( is_admin() || empty($search) || ! ( isset($qvars['s']) && isset($qvars['post_type']) && ! empty($qvars['s']) && $qvars['post_type'] === 'product' ) ) {通过以下:
Run Code Online (Sandbox Code Playgroud)if ( is_admin() || empty($search) || ! ( isset($qvars['s']) && ! empty($qvars['s']) ) ) {
对于 WooCommerce产品类别,您将替换:
$taxonomy = 'product_tag'; // WooCommerce product tag
Run Code Online (Sandbox Code Playgroud)
和:
$taxonomy = 'product_cat'; // WooCommerce product category
Run Code Online (Sandbox Code Playgroud)
对于 WooCommerce产品品牌,您将替换:
$taxonomy = 'product_tag'; // WooCommerce product tag
Run Code Online (Sandbox Code Playgroud)
和:
$taxonomy = 'product_brand'; // WooCommerce product Brands
Run Code Online (Sandbox Code Playgroud)
要启用搜索这两个产品类别术语和产品标签方面,您将使用:
add_filter( 'posts_search', 'woocommerce_search_product_tag_extended', 999, 2 );
function woocommerce_search_product_tag_extended( $search, $query ) {
global $wpdb, $wp;
$qvars = $wp->query_vars;
if ( is_admin() || empty($search) || ! ( isset($qvars['s'])
&& isset($qvars['post_type']) && ! empty($qvars['s'])
&& $qvars['post_type'] === 'product' ) ) {
return $search;
}
// Here set your custom taxonomies in the array
$taxonomies = array('product_tag', 'product_cat');
$tax_query = array('relation' => 'OR'); // Initializing tax query
// Loop through taxonomies to set the tax query
foreach( $taxonomies as $taxonomy ) {
$tax_query[] = array(
'taxonomy' => $taxonomy,
'field' => 'name',
'terms' => esc_attr($qvars['s']),
);
}
// Get the product Ids
$ids = get_posts( array(
'posts_per_page' => -1,
'post_type' => 'product',
'post_status' => 'publish',
'fields' => 'ids',
'tax_query' => $tax_query,
) );
if ( sizeof( $ids ) > 0 ) {
$search = str_replace( 'AND (((', "AND ((({$wpdb->posts}.ID IN (" . implode( ',', $ids ) . ")) OR (", $search);
}
return $search;
}
Run Code Online (Sandbox Code Playgroud)
代码位于活动子主题(或活动主题)的 functions.php 文件中。测试和工作。
为了让它在 WordPress 搜索上工作也替换:
Run Code Online (Sandbox Code Playgroud)if ( is_admin() || empty($search) || ! ( isset($qvars['s']) && isset($qvars['post_type']) && ! empty($qvars['s']) && $qvars['post_type'] === 'product' ) ) {通过以下:
Run Code Online (Sandbox Code Playgroud)if ( is_admin() || empty($search) || ! ( isset($qvars['s']) && ! empty($qvars['s']) ) ) {
新主题:将 WooCommerce 产品搜索扩展到自定义分类法和自定义字段