1

I'm creating a custom endpoint, that's basically this:

register_rest_route( 'wc/v3', '/custom-product-list(?:/(&P<custom_page>\d+))?(?:/(?P<custom_colors>\d+))?', array(
            'method' => 'GET',
            'callback' => 'custom_product_list',
            'args' => [
                'custom_page',
                'custom_colors'
            ],
        ));

What am trying to achieve is this: website.com/shop?page=2&colors=red,green,blue You know, like a normal URL that you can add stuff to it, and also have these things optional.

This however, doesn't work.

I decode it like this:

function custom_product_list( $args ) {
        global $product;


        $offset = 0;
        $limit = 9;

        $page = $args['custom_page'];
        $colors = $args['custom_colors'];

How should I go about doing this?

2
  • This should help: stackoverflow.com/questions/43986513/… Commented Jun 1, 2020 at 10:31
  • Am not using custom fields. This is WooCommerce and I need these values so I can put them inside the WP WooCommerce Query. That there is totally unrelated to this. Commented Jun 1, 2020 at 10:53

1 Answer 1

1

try add another parametr for args, like

'args' => array(
            'custom_page' => array(
                'required'          => true,

            ),
            'custom_colors' => array(
                'required'          => true,
            )
        ),
Sign up to request clarification or add additional context in comments.

3 Comments

and what does this do?
@kami not sure 100% but yesterday i have some problem with custom endpoint and continued to receive an error until he declared a description of the parameters
@kami this is used to validate the arguments.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.