基于 ACF 属性的 REST 过滤器不起作用

问题描述 投票:0回答:2

我有这个 REST 数据:

[{"id":215,"acf":{"stad":{"value":"barcelona","label":"barcelona"},"description":"","images":[{"ID":191,"id":191,"title":"logo-black.png","filename":"logo-black.png","filesize":3080,"url":"https:\/\/wordpress-132670-574369.cloudwaysapps.com\/wp-content\/uploads\/logo-black.png","link":"https:\/\/wordpress-132670-574369.cloudwaysapps.com\/logo-black-png\/","alt":"","author":"1","description":"","caption":"","name":"logo-black-png","status":"inherit","uploaded_to":0,"date":"2018-04-16 15:39:37","modified":"2018-08-05 15:19:12","menu_order":0,"mime_type":"image\/png","type":"image","subtype":"png","icon":"https:\/\/wordpress-132670-574369.cloudwaysapps.com\/wp-includes\/images\/media\/default.png","width":443,"height":98,"sizes":{"thumbnail":"https:\/\/wordpress-132670-574369.cloudwaysapps.com\/wp-content\/uploads\/logo-black-150x98.png","thumbnail-width":150,"thumbnail-height":98,"medium":"https:\/\/wordpress-132670-574369.cloudwaysapps.com\/wp-content\/uploads\/logo-black-300x66.png","medium-width":300,"medium-height":66,"medium_large":"https:\/\/wordpress-132670-574369.cloudwaysapps.com\/wp-content\/uploads\/logo-black.png","medium_large-width":443,"medium_large-height":98,"large":"https:\/\/wordpress-132670-574369.cloudwaysapps.com\/wp-content\/uploads\/logo-black.png","large-width":443,"large-height":98}}]}},{"id":205,"acf":{"stad":{"value":"oslo","label":"oslo"},"description":"","images":false,"myid":"333"}}]

我创建了一个过滤器来使用“myid”参数进行查询,如下所示:

/wp-json/wp/v2/hotels/?myid=333

这是我添加到 functions.php:

的过滤器代码
add_filter('rest_hotels_vars', function ($valid_vars)
{
    return array_merge($valid_vars, array('myid', 'meta_query'));
});


add_filter('rest_hotels_query', function($args, $request) 
{
    $myid = $request->get_param('myid');

    if (!empty( $myid)) 
    {
        $args['meta_query'] = array(
            array(
                'key'     => 'myid',
                'value'   => $myid,
                'compare' => '=',
            )
        );      
    }

    return $args;
}, 10, 2 );

酒店是自定义帖子类型。

查询始终返回所有行,过滤器不起作用。

这里出了什么问题?

php wordpress advanced-custom-fields wordpress-rest-api
2个回答
2
投票

如果您仔细查看过滤器,您就会明白错误。

apply_filters( "rest_{$this->post_type}_query", 数组 $args, WP_REST_Request $request );

这是您的代码:

add_filter('rest_hotels_query', function($args, $request){

$myid = $request->get_param('myid');

if (!empty( $myid)) 
{
    $args['meta_query'] = array(
        array(
            'key'     => 'myid',
            'value'   => $myid,
            'compare' => '=',
        )
    );      
}

return $args;

}, 10, 2 );

这里rest_hotels_query:我们需要输入帖子类型名称,请小心,如果您的帖子类型名称是酒店,那么过滤器应该类似于:“rest_hotel_query”

这是工作代码:

add_filter('rest_hotel_query', function($args, $request){
$myid = $request->get_param('myid');

if (!empty( $myid)) 
{
    $args['meta_query'] = array(
        array(
            'key'     => 'myid',
            'value'   => $myid,
            'compare' => '=',
        )
    );      
}

return $args;
}, 10, 2 );

与帖子控制器的收集参数相同的情况:

apply_filters( "rest_{$this->post_type}_query", 数组 $args, WP_REST_Request $request );

应该是:

   add_filter('rest_hotel_collection_params', function ($valid_vars){
    return array_merge($valid_vars, array('myid', 'meta_query'));
   });

0
投票

rest_query_vars
过滤器不再存在。看看 https://developer.wordpress.org/reference/hooks/rest_this-post_type_collection_params/https://developer.wordpress.org/reference/hooks/rest_this-post_type_query/

所以更换这个

add_filter('rest_hotels_vars', function ($valid_vars)
{
   return array_merge($valid_vars, array('myid', 'meta_query'));
});

有了这个

add_filter('rest_hotels_collection_params', function ($valid_vars)
{
   return array_merge($valid_vars, array('myid', 'meta_query'));
});

它应该可以工作。

© www.soinside.com 2019 - 2024. All rights reserved.