douci2015 2017-05-18 16:27
浏览 33

在自定义路由中访问WP自定义REST字段

I have created custom fields to display in WordPress rest api. I want the same data to be fetched into a custom route api. How it is possible?

That is, I have some custom rest fields in the path example.com/wp-json/wp/v2/posts/

have the following rest fields I need to show in the another route. ie at example.com/wp-json/mycustom/route/v2

How can I do it?

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',
   ));
});
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
    • ¥30 线性代数的问题,我真的忘了线代的知识了
    • ¥15 有谁能够把华为matebook e 高通骁龙850刷成安卓系统,或者安装安卓系统
    • ¥188 需要修改一个工具,懂得汇编的人来。
    • ¥15 livecharts wpf piechart 属性
    • ¥20 数学建模,尽量用matlab回答,论文格式
    • ¥15 昨天挂载了一下u盘,然后拔了
    • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
    • ¥20 易康econgnition精度验证
    • ¥15 msix packaging tool打包问题