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


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(

    // Meta
    foreach ($timeslip_meta_fields as $field) {
                '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')); Sep 5, 2019 at 15:52
  • Try using get_query_var() to get the query values.
    – dnavarrojr
    Sep 5, 2019 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 Sep 5, 2019 at 19:45

2 Answers 2


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:


Then you access to the request data as:


You originally set the requested data as:


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? Sep 5, 2019 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. Sep 5, 2019 at 19:42
  • @DaveMcCourt Looks like you're not settings the second argument for the filter, should be accept $args and $request. Sep 5, 2019 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. Sep 5, 2019 at 20:07
  • Yep the last filter param was missing. Works great now, thank you! Sep 5, 2019 at 20:49

In my case, I had to get the parameters with the get_query_params() method and I used a meta_query to set the values, looking like this:

function timeslip_meta_request_params( $args, $request ) {

    $query_params = $request->get_query_params();

    if ( isset( $query_params['meta_key'] ) && isset( $query_params['meta_value'] ) ) {
        $args['meta_query'] = array(
                'key'   => sanitize_text_field( $query_params['meta_key'] ),
                'value' => sanitize_text_field( $query_params['meta_value'] )

    return $args;

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.