在 WooCommerce 产品搜索中启用自定义分类法
Enable custom taxonomies in WooCommerce product search
我想要的: 修改 WooCommerce 搜索表单(在前端)的查询以通过搜索名称、描述和 product_tag 来显示产品产品。
我有: 我正在尝试使用此代码 inspired from this answer return 产品名称和描述的结果。但是,如果我使用标签名称进行搜索,则没有结果。搜索查询不在产品标签中搜索。
如何重现此问题(将下面的代码放入您的活动主题的 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 );
示例: 我有一种产品:带有产品标签“糖果”的黑巧克力。使用此代码,如果我在搜索表单中搜索“巧克力”,该产品将被 returned。但如果我搜索“糖果”:没有结果。
您的代码中有很多错误和错误(例如需要税务查询)。
要仅在 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;
}
代码进入您的活动子主题(或活动主题)的 functions.php 文件。已测试并有效。
To make it work On WordPress search too replace:
if ( is_admin() || empty($search) || ! ( isset($qvars['s'])
&& isset($qvars['post_type']) && ! empty($qvars['s'])
&& $qvars['post_type'] === 'product' ) ) {
By the following:
if ( is_admin() || empty($search) || ! ( isset($qvars['s']) && ! empty($qvars['s']) ) ) {
对于 WooCommerce 产品类别,您将替换:
$taxonomy = 'product_tag'; // WooCommerce product tag
与:
$taxonomy = 'product_cat'; // WooCommerce product category
对于 WooCommerce 产品品牌,您将替换:
$taxonomy = 'product_tag'; // WooCommerce product tag
与:
$taxonomy = 'product_brand'; // WooCommerce product Brands
对于多个分类法。
要启用对 产品类别术语和产品标签术语的搜索,您将使用:
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;
}
代码进入您的活动子主题(或活动主题)的 functions.php 文件。已测试并有效。
To make it work On WordPress search too replace:
if ( is_admin() || empty($search) || ! ( isset($qvars['s'])
&& isset($qvars['post_type']) && ! empty($qvars['s'])
&& $qvars['post_type'] === 'product' ) ) {
By the following:
if ( is_admin() || empty($search) || ! ( isset($qvars['s']) && ! empty($qvars['s']) ) ) {
新话题:
我想要的: 修改 WooCommerce 搜索表单(在前端)的查询以通过搜索名称、描述和 product_tag 来显示产品产品。
我有: 我正在尝试使用此代码 inspired from this answer return 产品名称和描述的结果。但是,如果我使用标签名称进行搜索,则没有结果。搜索查询不在产品标签中搜索。
如何重现此问题(将下面的代码放入您的活动主题的 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 );
示例: 我有一种产品:带有产品标签“糖果”的黑巧克力。使用此代码,如果我在搜索表单中搜索“巧克力”,该产品将被 returned。但如果我搜索“糖果”:没有结果。
您的代码中有很多错误和错误(例如需要税务查询)。
要仅在 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;
}
代码进入您的活动子主题(或活动主题)的 functions.php 文件。已测试并有效。
To make it work On WordPress search too replace:
if ( is_admin() || empty($search) || ! ( isset($qvars['s']) && isset($qvars['post_type']) && ! empty($qvars['s']) && $qvars['post_type'] === 'product' ) ) {
By the following:
if ( is_admin() || empty($search) || ! ( isset($qvars['s']) && ! empty($qvars['s']) ) ) {
对于 WooCommerce 产品类别,您将替换:
$taxonomy = 'product_tag'; // WooCommerce product tag
与:
$taxonomy = 'product_cat'; // WooCommerce product category
对于 WooCommerce 产品品牌,您将替换:
$taxonomy = 'product_tag'; // WooCommerce product tag
与:
$taxonomy = 'product_brand'; // WooCommerce product Brands
对于多个分类法。
要启用对 产品类别术语和产品标签术语的搜索,您将使用:
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;
}
代码进入您的活动子主题(或活动主题)的 functions.php 文件。已测试并有效。
To make it work On WordPress search too replace:
if ( is_admin() || empty($search) || ! ( isset($qvars['s']) && isset($qvars['post_type']) && ! empty($qvars['s']) && $qvars['post_type'] === 'product' ) ) {
By the following:
if ( is_admin() || empty($search) || ! ( isset($qvars['s']) && ! empty($qvars['s']) ) ) {
新话题: