Adding WordPress API Endpoint With Multiple Parameters

I have the same problem, i search on google finally with the help of the above answer i found out the solution. this may help others.

$this->base = home
register_rest_route( 

        $namespace, "https://wordpress.stackexchange.com/" . $this->base . "https://wordpress.stackexchange.com/" .  'products' . "https://wordpress.stackexchange.com/", array(
            array( 
                'methods'   => WP_REST_Server::READABLE, 
                'callback'  => array( $this, 'rest_api_popular_products'), 
            ),
        )  
    );        
    register_rest_route(
        $namespace, "https://wordpress.stackexchange.com/" . $this->base . "https://wordpress.stackexchange.com/" .  'products' . '/(?P<category>[\d]+)/(?P<sort>[\w]+)', array(
            'args'   => array(
                'id' => array(
                    'description' => __( 'Unique identifier for the resource.', 'woocommerce' ),
                    'type'        => 'integer',
                ),
            ),
            array(
                'methods'             => WP_REST_Server::READABLE,
                'callback' => array( $this, 'rest_api_popular_products' ),                  
                'args'                => array(
                    'context' => $this->get_context_param( array( 'default' => 'view' ) ),
                ),
            )
        )
    );

API request like : ..wp-json/wc/v2/home/products/?category=89&sort=popularity

Leave a Comment