Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用特定帖子类型访问所有内容(Wordpress)_Wordpress_Rest_Wordpress Rest Api - Fatal编程技术网

使用特定帖子类型访问所有内容(Wordpress)

使用特定帖子类型访问所有内容(Wordpress),wordpress,rest,wordpress-rest-api,Wordpress,Rest,Wordpress Rest Api,我有一个关于Wordpress+RESTAPI的问题, 我使用register\u rest\u route为我的收藏添加新的端点,我还创建了一个名为“article”的插件,该帖子类型的类型也是“article”。 现在我想在我的新端点中访问所有这些帖子类型,如下所示: function my_awesome_func( $post_id ) { $x = get_post_type_object( 'article' ); return $x[$post_id]; }

我有一个关于Wordpress+RESTAPI的问题, 我使用
register\u rest\u route
为我的收藏添加新的端点,我还创建了一个名为“article”的插件,该帖子类型的类型也是“article”。 现在我想在我的新端点中访问所有这些帖子类型,如下所示:

     function my_awesome_func( $post_id ) {
   $x = get_post_type_object( 'article' );
   return $x[$post_id];
 }
 add_action( 'rest_api_init', function () {
  register_rest_route( 'Articles/v2', '/author/(?P<id>\d+)', array(
    'methods' => 'GET',
    'callback' => 'my_awesome_func',
  ) );
} );
函数my\u awesome\u func($post\u id){
$x=获取发布类型对象(“文章”);
返回$x[$post_id];
}
添加操作('rest\u api\u init',函数(){
注册rest路由('Articles/v2','/author/(?P\d+),数组(
'方法'=>'获取',
“回调”=>“我的超级棒”,
) );
} );
我知道你不会写
$x[$post\u id]
。我写它是想说我想访问特定的文章id。我该怎么做?
谢谢。

我终于解决了这个问题,我发布了它,它可能对其他人有用

 add_action( 'rest_api_init', function () {
          register_rest_route( 'wp/v2/Articles', '/author/(?P<id>\d+)', array(
            'methods' => 'GET',
            'callback' => 'my_awesome_func',
          ) );
        } );
        function my_awesome_func( $data ) {
          $args = array(
            'post_type' => 'article',
            'p'         => $data['id'],
          );
          $query = new WP_Query( $args );

          return $query->post;
        }
add_操作('rest_api_init',函数(){
注册rest路由('wp/v2/Articles','/author/(?P\d+),数组(
'方法'=>'获取',
“回调”=>“我的超级棒”,
) );
} );
函数my_awesome_func($data){
$args=数组(
“post_type”=>“article”,
'p'=>$data['id'],
);
$query=新的WP\u查询($args);
返回$query->post;
}
我也尝试
获取元数据()
,但它不起作用。。