admin管理员组文章数量:1204016
My goal is to order a list of stars based on their distance from Earth, so I have created a "distance" field. The “distance” field is a meta field I’ve added. Looking at the WP API endpoints, I see that it isn’t one of the args under my custom post type endpoint, so I’ve been looking for examples of code that does exactly that. I’ve examined this gist, this blog post by Tim Ross, this post from Shishir Raven, and this StackOverflow question. So far, none of these solutions have worked, and in some places even appear contradictory to me (for example, that gist puts down “meta_value_num” into the $params['orderby']['enum'] value through the collection params filter hook, but Tim Ross and others put down the meta_key instead.) I really don’t know what to think or do here.
My understanding is that the "rest_api_init" hook is the one I would use to add this parameter to the REST API, but none of the techniques I have used do so. You can see below that "distance" (nor "meta_value" or "meta_value_num") appear in the orderby enum array. My API readout looks like this:
/wp/v2/stars:
namespace: "wp/v2"
endpoints:
0:
args:
orderby:
description: "Sort collection by post attribute."
type: "string"
default: "date"
enum:
0: "author"
1: "date"
2: "id"
3: "include"
4: "modified"
5: "parent"
6: "relevance"
7: "slug"
8: "include_slugs"
9: "title"
required: false
Adding screenshot just for clarity:
I'm rather stumped at this point. This is the code I've been using inside my plugin file to add the data to REST. I'm probably doing more than necessary, as I've been trying to comment out one thing and then the other to see what works, but nothing does.
// Adding custom fields to rest
add_action( 'rest_api_init', 'astrog_star_register_fields');
function astrog_star_register_fields(){
foreach (['spect', 'distance', 'astrog_lumos', 'astrog_hue'] as $field) {
register_rest_field('astrog_star',
$field,
[
'get_callback' => 'astrog_star_get_field',
'update_callback' => null,
'schema' => null,
]);
}
}
add_action('rest_api_init', 'astrog_star_orderby');
function astrog_star_orderby(){
add_filter("rest_endpoints", function($routes){
$route = $routes['/wp/v2/stars'];
$route['endpoints'][0]['args']['orderby']['enum'][] = 'meta_value';
$route['endpoints'][0]['args']['meta_key'] = [
'description' => 'The meta key to query',
'type' => 'float',
'enum' => ['distance'],
'validate_callback' => 'rest_validate_request_arg',
];
return $routes;
}, 10,1);
add_filter("rest_stars_collection_params", function( $params ){
$params['orderby']['enum'][] = "distance";
return $params;
},10,1);
add_filter("rest_stars_query", function($args, $request){
$orderkey = $request->get_param('orderby');
if( isset($orderkey) && 'distance' === $orderkey ) {
$args['meta_key'] = $orderkey;
$args['orderby']['enum'] = 'meta_value';
}
return $args;
},10,2);
}
/*
* Get the value of the meta fields
* @param array $object Details of current post.
* @param string $field_name Name of field.
* @param WP_REST_Request $request Current request
*
* @return mixed
*/
function astrog_star_get_field($object, $field_name, $request){
return get_post_meta($object['id'], $field_name, true);
}
function astrog_add_meta_vars ($current_vars) {
$current_vars = array_merge($current_vars, array('meta_key', 'meta_value', 'meta_value_num'));
return $current_vars;
}
add_filter('query_vars', 'astrog_add_meta_vars');
At this point I'm utterly stumped. I'm obviously missing something but I'm not sure what it is. The only thing I can think of right now is that perhaps it's a misnamed field; I originally created the field as "distance" and intend on later prefixing it with "astrog_" (as my plugin and project is called "Astrographer") for best practices; perhaps somewhere in there I did change it to "astrog_distance", but as far as I can tell I have not. Have I missed a REST API update that changes how to do this?
I am using Wordpress 5.9.2, and my repository can be found here. I'm grateful for any assistance I can get.
本文标签: Cannot add custom field to quotorderbyquot parameter in Rest API
版权声明:本文标题:Cannot add custom field to "orderby" parameter in Rest API 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738655727a2105129.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论