Php 具有动态值的自定义终结点不工作
我想为WooCommerce创建一个具有动态价值的新端点。不幸的是,我得到了Php 具有动态值的自定义终结点不工作,php,wordpress,woocommerce,woocommerce-rest-api,taxonomy-terms,Php,Wordpress,Woocommerce,Woocommerce Rest Api,Taxonomy Terms,我想为WooCommerce创建一个具有动态价值的新端点。不幸的是,我得到了rest\u no\u route错误 这是代码(插件): 这就是品牌属性在产品内部的外观: "brands": [ { "id": 1623, "name": "HUGO BOSS", "slug":
rest\u no\u route
错误
这是代码(插件):
这就是品牌
属性在产品内部的外观:
"brands": [
{
"id": 1623,
"name": "HUGO BOSS",
"slug": "hugo-boss"
}
],
谢谢你的帮助 这是关于分类术语(非后期元数据),尤其是WooCommerce品牌,因此您需要使用税务查询,如下所示(假设您使用的是WooCommerce品牌,因此分类应为“产品\品牌”) 因此,进行
WP\u查询的函数应该是:
function filter($data){
if( isset($data['brand']) && $data['brand'] > 0 ) {
$taxonomy = 'product_brand'; // The taxonomy for WooCommerce Brands
$products = (array) get_posts( array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => -1,
'tax_query' => array( array(
'taxonomy' => $taxonomy,
'field' => 'term_id',
'terms' => intval($data['brand']),
) ),
) );
// return an array of WP_Post Objects (or an empty array)
return $products;
}
}
它应该更有效。您应该使用自定义名称对函数进行不同的命名。非常感谢!
"brands": [
{
"id": 1623,
"name": "HUGO BOSS",
"slug": "hugo-boss"
}
],
function filter($data){
if( isset($data['brand']) && $data['brand'] > 0 ) {
$taxonomy = 'product_brand'; // The taxonomy for WooCommerce Brands
$products = (array) get_posts( array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => -1,
'tax_query' => array( array(
'taxonomy' => $taxonomy,
'field' => 'term_id',
'terms' => intval($data['brand']),
) ),
) );
// return an array of WP_Post Objects (or an empty array)
return $products;
}
}