我创建了自定义字段以在WordPress rest api中显示。我希望将相同的数据提取到自定义路由API中。怎么可能?
也就是说,我在路径example.com/wp-json/wp/v2/posts /
中有一些自定义的休息字段我需要在另一条路线中显示以下休息字段。即在example.com/wp-json/mycustom/route/v2
我该怎么做?
function list_subpages() {
$data = array();
$request = array();
$my_column = array();
global $wpdb;
$save_table = $wpdb->prefix.'saved_post';
$regid = $_REQUEST['save_key'] ;
$sql1 = "select postid FROM ".$save_table." WHERE regid='".$regid."';";
$result1 = $wpdb->get_results($sql1);
foreach ($result1 as $p)
{
$id[]=$p->postid;
}
$args = array(
'post__in' => $id,
'per_page' => $per_page,
);
$subpages = get_posts( $args );
if ( empty( $subpages ) ) {
return null;
}
foreach ($subpages as $p) {
$request[] = $data;
}
return new WP_REST_Response($request, 200);
}
add_action('rest_api_init', function () {
$namespace = 'savedpost/v2';
$base = 'user';
register_rest_route($namespace, '/' . $base, array(
'methods' => 'GET',
'callback' => 'list_subpages',
));
});