Monday, February 13, 2023
HomeWordPress DevelopmentWordpress REST API will not permit me to filter by writer ID...

WordPress REST API will not permit me to filter by writer ID when referred to as internally, works externally in Postman


I am at present making a customized API endpoint for a website that I am constructing, which works completely high-quality other than this situation with writer ID’s.

If I do a GET request to web site.native/wp-json/wp/v2/podcasts it returns legitimate JSON of all the podcasts Customized Submit Sort. If I do a GET request to web site.native/wp-json/wp/v2/podcasts?writer=1 it additionally returns legitimate JSON of all the podcasts Customized Submit Sort – no issues right here.

The issue nonetheless comes from another code that I am writing, I am making an attempt to do that in a customized perform:

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

After I run it in Postman I get the next response:

{
    "code": "rest_no_route",
    "message": "No route was discovered matching the URL and request technique.",
    "knowledge": {
        "standing": 404
    }
}

If I do that name internally contained in the perform, it really works high-quality:

$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 );

I can’t work out why it is producing that error when a direct name to it really works high-quality?

EDIT:
To offer extra context, that is the perform I am utilizing:

perform get_all_posts(WP_REST_Request $request){
    $server = rest_get_server();

    if($request->get_param('id')){
        // $writer = $request->get_param('writer');
        $podcastsRequest = new WP_REST_Request( 'GET', '/wp/v2/podcasts?writer=1');
        // $articlesRequest = new WP_REST_Request( 'GET', '/wp/v2/articles?writer=1');
        // $webinarsRequest = new WP_REST_Request( 'GET', '/wp/v2/webinars?writer=1');
        // $expertInterviewsRequest = new WP_REST_Request( 'GET', '/wp/v2/expert-interviews?writer=1');
        // $guidesRequest = new WP_REST_Request( 'GET', '/wp/v2/guides?writer=1');
    } 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' );
    }

    $podcastsResponse = rest_do_request( $podcastsRequest );
    $podcastsData = $server->response_to_data( $podcastsResponse, false );


    $articlesResponse = rest_do_request( $articlesRequest );
    $articlesData = $server->response_to_data( $articlesResponse, false );

    $webinarsResponse = rest_do_request( $webinarsRequest );
    $webinarsData = $server->response_to_data( $webinarsResponse, false );

    $expertInterviewsResponse = rest_do_request( $expertInterviewsRequest );
    $expertInterviewsData = $server->response_to_data( $expertInterviewsResponse, false );

    $guidesResponse = rest_do_request( $guidesRequest );
    $guidesData = $server->response_to_data( $guidesResponse, false );

    $merged = array_merge(
        $podcastsData,
        $articlesData,
        $webinarsData,
        $expertInterviewsData,
        $guidesData,
    );

    return $merged;
}

add_action('rest_api_init', perform () {
    register_rest_route( 'wp/v2', '/all-posts', [
        'methods'  => 'GET',
        'callback' => 'get_all_posts',
    ]);
});

As I’ve mentioned above, if I simply use the new WP_REST_Request( 'GET', '/wp/v2/podcasts' ); endpoints, it really works high-quality, but when I try to use new WP_REST_Request( 'GET', '/wp/v2/podcasts?writer=1' ); it returns that error, regardless of a direct name to web site.native/wp-json/wp/v2/podcasts?writer=1 returning all of the posts for the writer.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments