admin管理员组文章数量:1122846
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 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!
Share Improve this question asked Sep 5, 2019 at 14:57 Dave McCourtDave McCourt 581 silver badge10 bronze badges 3 |3 Answers
Reset to default 1Add 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:
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;
}
A little correction to Everlado Matia's answer:
if (!function_exists('yourtheme_custom_meta_request_params')) {
function yourtheme_custom_meta_request_params($args, $request)
{
if (isset($request["filter"]['meta_key']) && isset($request["filter"]['meta_value'])) {
$args['meta_query'] = array(
[
'key' => sanitize_text_field($request["filter"]['meta_key']),
'value' => sanitize_text_field($request["filter"]['meta_value'])
]
);
}
return $args;
}
}
本文标签: REST API and filtering by meta value
版权声明:本文标题:REST API and filtering by meta value 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736309015a1933866.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
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 Commented Sep 5, 2019 at 19:45