我想为 WooCommerce 创建一个具有动态价值的新端点。不幸的是我出错了rest_no_route。
这是代码(插件):
<?php
function filter($data){
$brand = $data['brand'];
$products = [];
$loop = new WP_Query( array(
'post_type' => 'product',
'posts_per_page' => -1,
'meta_query' => array( array(
'key' => 'brands.id',
'value' => $brand,
'compare' => '=',
)),
));
if ( $loop->have_posts() ){
while ( $loop->have_posts() ){
$loop->the_post();
array_push($products, get_post())
}
wp_reset_postdata();
}
return $products;
}
add_action( 'rest_api_init', function () {
register_rest_route( '/wc/v3', '/brand=(?P<brand>\d+), array(
'methods' => 'GET',
'callback' => 'brand',
) );
} );
?>
这就是我尝试访问的方式:
/wp-json/wc/v3/brand=1623
这是brand产品内部属性的样子:
"brands": [
{
"id": 1623,
"name": "HUGO BOSS",
"slug": "hugo-boss"
}
],
感谢您的每一个帮助!
慕雪6442864