regex – Register GET REST API route with multiple parameters

Question

I’m trying to register a GET REST API route with multiple parameters with the following code:

register_rest_route( 'myplugin/v1', '/posts/?number=(?P<number>[d]+)&amp;offset=(?P<offset>[d]+)&amp;total=(?P<total>[d]+)', array(
    'methods'             => 'GET',
    'callback'            => 'my_rest_function',
    'permission_callback' => '__return_true',
    'args'                => array(
        'number' => array(
            'validate_callback' => function( $param, $request, $key ) {
                return is_numeric( $param );
            }
        ),
        'offset' => array(
            'validate_callback' => function( $param, $request, $key ) {
                return is_numeric( $param );
            }
        ),
        'total' => array(
            'validate_callback' => function( $param, $request, $key ) {
                return is_numeric( $param );
            }
        ),
    ),
) );

But, when I call it using for example:

https://example.com/wp-json/myplugin/v1/posts/?number=3&offset=0&total=3

I’m getting a No route was found matching the URL and request method. error.

What am I doing wrong?

0
leemon 4 weeks 2021-03-21T04:19:21-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse