0

I have a posttype 'timeslip' and I am calling this with the following API endpoint:

/wp-json/wp/v2/timeslip?meta_key=user_id&meta_value=1

I have exposed my custom field user_id to the API meta and can see it in the response.

add_filter('rest_timeslip_query', array($this, 'timeslip_meta_request_params'));

 public function register_meta_api() {

    // Meta Fields that should be added to the API 
    $timeslip_meta_fields = array(
        'user_id',
        'total_time',
        'start_time',
        'end_time',
    );

    // Meta
    foreach ($timeslip_meta_fields as $field) {
        register_rest_field('timeslip',
            $field,
            array(
                'get_callback'    =>  array($this, 'get_meta'),
                'update_callback' => array($this, 'update_meta'),
                'schema'          =>  null,
            )
        );
    }

}

public function get_meta($object, $field_name) {

    return get_post_meta($object['id'], $field_name);

}

However nothing is filtered.

public function timeslip_meta_request_params($args, $request) {

    $args['meta_key'] = $request['timeslip_user_id'];
    $args['meta_value'] = intval($request['timeslip_user_id']);

    $valid_vars = array_merge( $args, array( 'meta_key', 'meta_value' ) );
    return $valid_vars;


    // $args['meta_key'] = 'user_id';
    // $args['meta_value'] = 1;

    return $args;

}

If I set the $args manually then it works so I guess something is not being passed in.

Reading around it seems the documentation has changed a lot it is hard to find the right way to do this.

Any help much appreciated!

  • I should add that I the filter is added: add_filter('rest_timeslip_query', array($this, 'timeslip_meta_request_params')); – Dave McCourt Sep 5 at 15:52
  • Try using get_query_var() to get the query values. – dnavarrojr Sep 5 at 16:34
  • Thanks @dnavarrojr but I get the same error Fatal error: Uncaught ArgumentCountError: Too few arguments to function TimeslipPlugin::timeslip_meta_request_params(), 1 passed in ... as reported below in Roel's answer – Dave McCourt Sep 5 at 19:45
1

Add a third parameter in the get_post_meta() function as true, that will return the single value of the current post meta, if you don't set it it will return an array of the values.

What is happening now is that internally the query is trying to compare the meta_value with an array instead of a string or integer and that's why you don't get any results.

Also, your requested data should be the same name as the query params indicated in the current endpoint, if you put:

/wp-json/wp/v2/timeslip?meta_key=user_id&meta_value=1

Then you access to the request data as:

$request['meta_key'];
$request['meta_value'];

You originally set the requested data as:

$request['timeslip_user_id'];
$request['timeslip_user_id'];

So your query filter function should looks like this:

function timeslip_meta_request_params( $args, $request ) {
    $args['meta_key']   = $request['meta_key'];
    $args['meta_value'] = $request['meta_value'];

    return $args;
}

I just tested the code in a local env, check that the user_id field is an string. Hope if works for you:

enter image description here

  • Thanks @roel but I get the following error: Fatal error: Uncaught ArgumentCountError: Too few arguments to function TimeslipPlugin::timeslip_meta_request_params(), 1 passed in ... Any ideas? – Dave McCourt Sep 5 at 19:34
  • BTW sorry for the typo with the user_id. I changed it to timeslip_user_id in case there was some global var clash but didn't update all the code here. – Dave McCourt Sep 5 at 19:42
  • @DaveMcCourt Looks like you're not settings the second argument for the filter, should be accept $args and $request. – Roel Magdaleno Sep 5 at 20:01
  • 1
    The filter should be: add_filter('rest_timeslip_query', array($this, 'timeslip_meta_request_params'), 10, 2);, the last param of the filter is 2, meaning that accepts two arguments in your filter function. – Roel Magdaleno Sep 5 at 20:07
  • Yep the last filter param was missing. Works great now, thank you! – Dave McCourt Sep 5 at 20:49

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.