Ale*_*x S 4 wordpress woocommerce woothemes
我刚刚继承了一个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
)
)
Run Code Online (Sandbox Code Playgroud)
我的印象是我可以使用 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' => '='
)
)
Run Code Online (Sandbox Code Playgroud)
);
我在这方面尝试了很多变体,但没有一个起作用。有什么建议?
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
其中 Honda 是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
);
Run Code Online (Sandbox Code Playgroud)
如果你是指在文档,上面的说法是这同本
$args = array(
'post_type' => 'product',
'tax_query' => array(
array(
'taxonomy' => 'pa_brand',
'field' => 'slug',
'terms' => 'honda',
),
),
);
Run Code Online (Sandbox Code Playgroud)
编辑:Woocommerce 属性是分类法而不是自定义字段,
您需要使用tax_query,而不是meta_query,分类法下保存wp_term_taxonomy和wp_terms数据库表,虽然meta_query是对象查询基于元场/自定义字段值都保存在wp_postmeta数据库中的表,
https://codex.wordpress.org/Class_Reference/WP_Query
| 归档时间: |
|
| 查看次数: |
3415 次 |
| 最近记录: |