访问具有特定帖子类型的所有内容(Wordpress)

时间:2018-09-07 17:11:52

标签: wordpress rest wordpress-rest-api

我有一个关于Wordpress + rest api的问题, 我使用register_rest_route为集合添加了新的端点,并且我还创建了一个名为“ article”的插件,该帖子类型的类型也为“ article”。 现在,我想使用GET这样在我的新端点中访问所有这些帖子类型:

     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',
  ) );
} );

我知道您不会写$x[$post_id]。我写这封信的目的是想访问特定的文章ID。我该怎么办? 谢谢。

1 个答案:

答案 0 :(得分:1)

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

 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;
        }