2
With this example我设法在WordPress REST API中添加自定义帖子类型和自定义元数据。WordPress REST API - 隐藏未登录用户的字段
现在,我试图隐藏一些数据对于非登录用户,我想是这样的:
add_filter('rest_prepare_the_event', 'add_meta_to_json', 10, 3);
function add_meta_to_json($data, $post, $request){
$response_data = $data->get_data();
if ($request['context'] !== 'view' || is_wp_error($data)) {
return $data;
}
$start = get_post_meta($post->ID, '_the_event_start', true);
if(empty($start)){
$start = '';
}
$end = get_post_meta($post->ID, '_the_event_end', true);
if(empty($end)){
$end = '';
}
$location = get_post_meta($post->ID, '_the_event_location', true);
if(empty($location)){
$location = '';
}
if($post->post_type == 'the_event'){
$response_data['event_meta'] = array(
'start' => $start,
'end' => $end,
'location' => $location,
);
if (!is_user_logged_in()) {
unset($response_data['event_meta']['location']);
}
}
$data->set_data($response_data);
return $data;
}
在上面的示例代码中,我试图隐藏位置场没有登录的用户,但这是行不通的。
如何为非登录用户隐藏该字段?
不工作,我登录和现场不显示 – akuljana