• Resolved fersamp

    (@fersamp)


    Hi,
    I have problem with register_routes(): I have to do a query to get some thing but I don’t manage to pass a date to my function.

    This is my route registration:

    register_rest_route( $this->namespace, '/' . $this->rest_base . '/todayEvents', array(
    		array(
    		'methods'         => WP_REST_Server::READABLE,
    		'callback'        => array( $this, 'getTodayEvents' ),
    		'args'            => array(
    							 'posts_per_page'     => 4,
    							 'offset'     => 1,
    							 'filter'     => date("Y-m-d")
    							),
    		),
    		'schema'          => array( $this, 'get_public_item_schema' ),
    		) );

    In getTodayEvents($request) method I need to retrieve the date passed but I don’t know how.

    In my function
    $dateFilter = $request['filter'];

    $dateFilter is empty.

    I need to use the same function and passing different params because I have to filter my results by different date, so I think it’s better to use a generic function and passing different arguments.

    Thank you

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Daniel Bachhuber

    (@danielbachhuber)

    The default values aren’t present in the request because you’re registering your arguments incorrectly. Please review the documentation thoroughly.

    Your argument registration should be something like this (untested):

    'args' => array(
    	'posts_per_page' => array(
    		'type'    => 'integer',
    		'default' => 4,
    	),
    	'offset' => array(
    		'type'    => 'integer',
    		'default' => 1,
    	),
    	'filter' => array(
    		'type'    => 'string',
    		'default' => date("Y-m-d"),
    	),
    )
    Plugin Author Daniel Bachhuber

    (@danielbachhuber)

    Sorry. Just to confirm, you should be using WP-API v2 and WordPress 4.4 or higher. This support forum is for the deprecated WP-API v1.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Problem with parameter in register route method’ is closed to new replies.