gpt4 book ai didi

wordpress - 按自定义属性对象过滤 WooCommerce 产品

转载 作者:行者123 更新时间:2023-12-04 12:19:00 33 4
gpt4 key购买 nike

我刚刚继承了一个woocommerce项目,我需要将主页更改为仅显示特定品牌。他们设置了 Product-Data => Attribute => pa_brand。

如果我打印 pa_brand 数组,它会显示以下内容:

Array
(
[0] => stdClass Object
(
[term_id] => 1134
[name] => Name Brand
[slug] => name-brand
[term_group] => 0
[term_taxonomy_id] => 1134
[taxonomy] => pa_brand
[description] =>
[parent] => 0
[count] => 68
[object_id] => 3385
[filter] => raw
)
)

我的印象是我可以使用 pa_brand 使用键值对之一(最好是 slug)来过滤查询,但我不确定如何执行此操作。我发现的所有示例都没有对象,只有字符串结果:
$args = array(
'post_type' => array('product', 'product_variation'),
'posts_per_page' => 3,
'orderby' => 'rand',
'meta_query' => array(
array(
'key' => 'pa_brand',
'value' => array('slug' => 'brand-name'),
'compare' => '=',
),
array(
'key' => '_stock_status',
'value' => 'instock',
'compare' => '='
)
)

);

我在这方面尝试了很多变体,但没有一个起作用。有什么建议?

最佳答案

Woocommerce 属性是分类法,

假设您将创建一个 Brand 属性,则 url 结构是这样的,
yoursite.com/wp-admin/edit-tags.php?taxonomy=pa_brand&post_type=product
你看到分类名称是 pa_brand
现在,如果你在该分类下创建一个像本田这样的品牌,网址是这样的,
yoursite.com/wp-admin/edit-tags.php?action=edit&taxonomy=pa_brand&tag_ID=6&post_type=product
本田是 pa_brand 下的标签标签 ID 为 6 的分类法

现在要在特定分类下进行 woocommerce 查询,

我们可以使用 WP_query

我们可以使用这样的参数,

$args = array( 
'post_type' => 'product',
'taxonomy' => 'pa_brand', // This is the taxonomy slug for brand taxonomy
'term' => 'honda' // This is terms slug of the Honda Brand
);

如果您引用文档,上面的论点与 this 相同
$args = array(
'post_type' => 'product',
'tax_query' => array(
array(
'taxonomy' => 'pa_brand',
'field' => 'slug',
'terms' => 'honda',
),
),
);

编辑:
Woocommerce 属性是分类法而不是自定义字段,

您需要使用 tax_query而不是 meta_query ,分类保存在 wp_term_taxonomy 下和 wp_terms数据库表,而 meta_query用于基于保存在 wp_postmeta 下的元字段/自定义字段值的对象查询数据库表,

https://codex.wordpress.org/Class_Reference/WP_Query

关于wordpress - 按自定义属性对象过滤 WooCommerce 产品,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33790364/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com