Wordpress 将wp rest api posts请求限制为postID';s
我的整个项目需要将最近的帖子返回到给定的地理位置 我成功地做到了这一点,构建了一个自定义回调函数来查找最近的帖子,同时还构建了一个自定义数组,其中包含最近帖子的数据字段。如果我将此函数定义为路由的回调函数,那么一切都非常有效 好吧,但我希望返回的是默认的帖子返回的副本,当然只限于最近的帖子。而不是一组自定义的字段 我似乎不知道该怎么做 在讨论细节之前,我想知道该走哪条路。我的计划不起作用,是:Wordpress 将wp rest api posts请求限制为postID';s,wordpress,rest,include,Wordpress,Rest,Include,我的整个项目需要将最近的帖子返回到给定的地理位置 我成功地做到了这一点,构建了一个自定义回调函数来查找最近的帖子,同时还构建了一个自定义数组,其中包含最近帖子的数据字段。如果我将此函数定义为路由的回调函数,那么一切都非常有效 好吧,但我希望返回的是默认的帖子返回的副本,当然只限于最近的帖子。而不是一组自定义的字段 我似乎不知道该怎么做 在讨论细节之前,我想知道该走哪条路。我的计划不起作用,是: 扩展WP\u REST\u Posts\u控制器 注册(并初始化)新路由 在此新路由中定义自定义回
- 扩展WP\u REST\u Posts\u控制器
- 注册(并初始化)新路由
- 在此新路由中定义自定义回调函数
- 自定义回调函数构建一个最近的postID数组(http请求中传递了lat&long)
postID
s数组传递给WP\u REST\u Posts\u控制器的get\u items
方法,以获取特定(最近)Posts的“默认”返回
显然,像这样简单的方法是行不通的
public function custom_callback_function($geoloc_array) {
// call the function get_nearest_items($geoloc_array)
// that returns an array of postIDs'
// something like : [163,49]
$nearest_poi_array = get_nearest_items($geoloc_array);
// put the array of postIDs to fetch in "include" of a new array
// hand over new array to the get_items method of WP_REST_posts_controller
$posts_to_return['include'] = $nearest_poi_array;
return get_items($posts_to_return);
}
如果有人能给我指出正确的方向,我将不胜感激