- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我要的是:修改 WooCommerce 搜索表单的查询(在前端),通过搜索产品的名称、描述和产品标签来显示产品。
我有什么:我正在尝试使用此代码 inspired from this answer返回产品名称和描述的结果。但是,如果我使用标签名称进行搜索,则没有结果。搜索查询不搜索产品的标签。
如何重现此问题(将以下代码放在事件主题的 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 );
示例:我有一种产品:带有产品标签“糖果”的黑巧克力。使用此代码,如果我在搜索表单中搜索“巧克力”,产品将被返回。但如果我搜索“甜点”:没有结果。
最佳答案
您的代码中有很多错误和错误(例如需要税务查询)。
包括 仅 WooCommerce 产品搜索中的 WooCommerce 产品标签术语 (前端)使用以下内容:
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']) ) ) {
$taxonomy = 'product_tag'; // WooCommerce product tag
和:
$taxonomy = 'product_cat'; // WooCommerce product category
$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']) ) ) {
关于php - 在 WooCommerce 产品搜索中启用自定义分类法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63131123/
我需要创建嵌套分类法。 (用于深入搜索) 示例:我有一个词汇表: 汽车 -梅赛德斯 --SLK --时钟 -宝马 --7系列 --3系列 -阿斯顿·马丁 --DB9 --DB7 我想要做的是有 3 个
我正在创建一个 Wordpress 博客(还没有实时链接,因为它仍处于规划阶段)。我想设置以下标签结构: 第 1 类:服务标签:网页设计、标志设计、打印设计等。 第 2 类:客户类型标签:小企业, 大
我正在尝试翻译 Hugo 中的标签。 我的标签仅在我的帖子的元数据中声明: --- author: arthur.sw date: 2007-01-01T00:00:35Z tags: - exp
我的产品目录是一个经典的树状层次结构,具有不同级别的子分组。在子分组的每个级别,产品将共享 type:value 的属性。在树的最底层,将有继承了上层所有属性的产品,加上自己独特的属性。 我设想使用两
我正在做一个项目,想创建一个与 Wordpress 具有的分类功能类似的功能。 虽然我不太确定这一切是如何运作的。 他们有 3 个相关的表: wp_terms ( term_id, nam
我正在寻找嵌入式处理器的分类法。哪些处理器属于哪些家族?谁从谁下来? 这里有人知道描述它的简单文章吗? 最佳答案 Common Microcontrollers维基百科页面可能是一个起点,或者this
我有以下 PowerShell,它返回存储在 SharePoint 托管元数据中的所有部门的列表: Add-PSSnapin Microsoft.Sharepoint.Powershell # Get
我希望角色 Editor 能够访问所有 woocommerce 管理,我通过向该角色添加功能来设法做到这一点: $role = get_role( 'editor' ); $role-
我是 plone 和内容管理工具的新手。 我们在我们实验室的出版物列表所在的位置安装了一个plone。 我只是想要一些类似 EEA 导航的演示,我可以在其中定义例如作者小部件 field 小部件 以及
我正在建立一个汽车服务数据库。每个汽车服务都可以维修多种型号的汽车。 存储汽车服务和类型之间的关系以便以后能够查询的最佳方式是什么? 我的选择是: 对于每辆车,将模型保存为 post_meta 作为单
如何在批量导入/删除后更新 WordPress 分类法(类别/标签)的计数字段? 相关问题: WordPress › 支持 » 导入后修复评论和类别计数 http://wordpress.org/su
我正在尝试使用 WordPress REST API 获取分类列表。点击 /wp-json/wp/v2/taxonomies/post_tag 工作正常,但我还有一个名为 location 的自定义分
我正在使用 C# 更新管理元数据字段。 以下是获取 term 的 TermId 的代码。 string termId = string.Empty; try
我是一名优秀的程序员,十分优秀!