wordpress API不允许我在内部调用时按作者ID过滤,在Postman外部工作

qgelzfjb  于 2023-02-15  发布在  WordPress
关注(0)|答案(1)|浏览(96)

我目前正在为我正在构建的一个站点制作一个自定义API端点,除了作者ID的这个问题之外,它工作得非常好。
如果我向website.local/wp-json/wp/v2/podcasts发出GET请求,它会返回所有播客自定义帖子类型的有效JSON。如果我向website.local/wp-json/wp/v2/podcasts?author=1发出GET请求,它会返回所有播客自定义帖子类型的有效JSON-这里没有问题。
问题来自我正在编写的其他代码,我尝试在一个自定义函数中这样做:

$server = rest_get_server();
$podcastsRequest = new WP_REST_Request( 'GET', '/wp/v2/podcasts?author=1');
$podcastsResponse = rest_do_request( $podcastsRequest );
$podcastsData = $server->response_to_data( $podcastsResponse, false );

当我在Postman中运行它时,我得到以下响应:

{
    "code": "rest_no_route",
    "message": "No route was found matching the URL and request method.",
    "data": {
        "status": 404
    }
}

如果我在函数内部调用这个函数,它就能正常工作:

$server = rest_get_server();
$podcastsRequest = new WP_REST_Request( 'GET', '/wp/v2/podcasts');
$podcastsResponse = rest_do_request( $podcastsRequest );
$podcastsData = $server->response_to_data( $podcastsResponse, false );

我不明白为什么直接调用它可以正常工作,但它却产生了那个错误?

kyxcudwk

kyxcudwk1#

我的处理方法是错误的,我没有正确理解错误。在再次查看之后,我能够通过替换以下代码来修复它:

if($request->get_param('id')){
    // $author = $request->get_param('author');
    $podcastsRequest = new WP_REST_Request( 'GET', '/wp/v2/podcasts?author=1');
    $articlesRequest = new WP_REST_Request( 'GET', '/wp/v2/articles?author=1');
    $webinarsRequest = new WP_REST_Request( 'GET', '/wp/v2/webinars?author=1');
    $expertInterviewsRequest = new WP_REST_Request( 'GET', '/wp/v2/expert-interviews?author=1');
    $guidesRequest = new WP_REST_Request( 'GET', '/wp/v2/guides?author=1');
} else {
    $podcastsRequest = new WP_REST_Request( 'GET', '/wp/v2/podcasts?author=1' );
    $articlesRequest = new WP_REST_Request( 'GET', '/wp/v2/articles?author=1' );
    $webinarsRequest = new WP_REST_Request( 'GET', '/wp/v2/webinars?author=1' );
    $expertInterviewsRequest = new WP_REST_Request( 'GET', '/wp/v2/expert-interviews?author=1' );
    $guidesRequest = new WP_REST_Request( 'GET', '/wp/v2/guides?author=1' );
}

使用此代码:

if($request->get_param('author')){
    $author = $request->get_param('author');

    $podcastsRequest = new WP_REST_Request( 'GET', '/wp/v2/podcasts');
    $podcastsRequest->set_query_params( array( 'author' => $author ));

    $articlesRequest = new WP_REST_Request( 'GET', '/wp/v2/articles');
    $articlesRequest->set_query_params( array( 'author' => $author ));

    $webinarsRequest = new WP_REST_Request( 'GET', '/wp/v2/webinars');
    $webinarsRequest->set_query_params( array( 'author' => $author ));

    $expertInterviewsRequest = new WP_REST_Request( 'GET', '/wp/v2/expert-interviews');
    $expertInterviewsRequest->set_query_params( array( 'author' => $author ));

    $guidesRequest = new WP_REST_Request( 'GET', '/wp/v2/guides');
    $guidesRequest->set_query_params( array( 'author' => $author ));
} else {
    $podcastsRequest = new WP_REST_Request( 'GET', '/wp/v2/podcasts');
    $articlesRequest = new WP_REST_Request( 'GET', '/wp/v2/articles');
    $webinarsRequest = new WP_REST_Request( 'GET', '/wp/v2/webinars');
    $expertInterviewsRequest = new WP_REST_Request( 'GET', '/wp/v2/expert-interviews');
    $guidesRequest = new WP_REST_Request( 'GET', '/wp/v2/guides');
}

现在,API端点没有直接设置为匹配自身的其他参数,但是使用set_query_params()传递这些参数就完成了任务!

相关问题