0

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?

1

You don't need to include query parameters in the endpoint. Just the path:

register_rest_route( 'myplugin/v1', '/posts', 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 );
            }
        ),
    ),
) );
2
  • Is the args parameter necessary, then?
    – leemon
    Mar 21 at 19:40
  • 1
    If you want to require or validate certain arguments, then it can be helpful, yes. Mar 21 at 23:00
0

The request should be a regular expression, but not HTML encoded. So, instead of &amp; simply use &.

Also, the ? at the start of the URL query is interpreted as part of the regular expression, just meaning that the preceding / is optional. You have to escape it:

\/posts\/?number=(?P[\d]+)&offset=(?P[\d]+)&total=(?P[\d]+)

To be absolutely sure you can escape the slashes, too, but AFAIK this is not necessary here.

You can test your regex at Regex101.

2
  • I replaced the &amp;s with & and I'm still getting the No route was found matching the URL and request method error. I'm calling the register_rest_route function in the rest_api_init hook.
    – leemon
    Mar 21 at 10:18
  • @leemon sorry I updated my answer Mar 21 at 12:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.