我已经设法创建自定义api来显示这样的事件地点的所有数据。问题是如何按ID过滤地点?
地方列表的网址如下: www.mysite.com/wp-json/places/v1/list
我想通过ID过滤,如下所示: www.mysite.com/wp-json/places/v1/list?id=123
<?php
function list_places() {
$data = array();
$args = array(
'post_type' => 'event_places',
'posts_per_page' => -1
);
$places = get_posts($args);
$places_output = array();
foreach ($places as $place) :
$place_id = $place->ID;
$places_output[] = array(
'ID' => $place_id,
'title' => $place->post_title,
'adress' => get_field('address', $place_id),
'zip' => get_field('zipcode', $place_id),
'area' => get_field('area', $place_id)
);
endforeach;
$data = $places_output;
return $data;
}
add_action('rest_api_init', function () {
$namespace = 'places/v1';
$base = 'list';
register_rest_route($namespace, '/' . $base, array(
'methods' => 'GET',
'callback' => 'list_places',
));
});