WP REST API从post类型获取post


WP REST API fetch posts from post type

如何从特定的自定义帖子类型与WP REST API (v1或v2)获得所有帖子?我对这个很陌生,正在努力理解如何做到这一点。

我目前正在使用WP REST API v2,并设法通过此

获取所有帖子类型的列表
http://domain.com/wp-json/wp/v2/types

,然后设法获得我感兴趣的帖子类型

http://domain.com/wp-json/wp/v2/types/the-icons-update

如何从该特定内容类型获得所有帖子?

我试过了

http://domain.com/wp-json/wp/v2/posts?filter[post_type]=the-icons-update

但它返回一个空数组(我想它返回默认的帖子和在我的网站上只有帖子内的自定义帖子类型,我试图检索)。

我如何注册帖子类型会有问题吗?

function custom_post_type() {
$labels = array(
    'name'               => _x( 'The Icons Update', 'post type general name' ),
    'singular_name'      => _x( 'The Icons Update', 'post type singular name' ),
    'add_new'            => _x( 'Add Page', 'magazine' ),
    'add_new_item'       => __( 'Add New Page' ),
    'edit_item'          => __( 'Edit Page' ),
    'new_item'           => __( 'New Page' ),
    'all_items'          => __( 'All Pages' ),
    'view_item'          => __( 'View Page' ),
    'search_items'       => __( 'Search Pages' ),
    'not_found'          => __( 'No Page found' ),
    'not_found_in_trash' => __( 'No Page found in the Trash' ), 
    'parent_item_colon'  => '',
    'menu_icon'          => '',
    'menu_name'          => 'The Icons Update'
);
$args = array(
    'labels'        => $labels,
    'description'   => 'Holds our projects and project specific data',
    'public'        => true,
    'menu_position' => 5,
    'supports'      => array( 'title', 'editor', 'thumbnail', 'excerpt', 'custom-fields' ),
    'has_archive'   => true,
    'taxonomies'    => array('post_tag', 'category'),
    'hierarchical'  => false,
    'query_var'     => true,
    'queryable' => true,
        'searchable'    => true,
    'rewrite'       => array( 'slug' => 'the-icons-update' )
);
register_post_type( 'magazine', $args );
flush_rewrite_rules();
}
add_action( 'init', 'custom_post_type' );

对于v.2来说有一个非常简单的方法。您所要做的就是将以下属性包含到args数组中:'show_in_rest' => true

的例子:

register_post_type( 'recipe',
     array(
          'labels' => $labels,
          'public' => true,
          'menu_position' => 5,
          'hierarchical' => false,
          'supports' => $supports,
          'show_in_rest' => true,
          'taxonomies' => array('recipe-type', 'post_tag'),
          'rewrite' => array( 'slug' => __('recipe', 'recipe') )
     )
);

使用v2的REST API插件:

在主题的functions.php文件中,添加以下内容来创建rest端点:
add_action( 'init', 'add_myCustomPostType_endpoint');
function add_myCustomPostType_endpoint(){
    global $wp_post_types;
    $wp_post_types['myCustomPostType']->show_in_rest = true;
    $wp_post_types['myCustomPostType']->rest_base = 'myCustomPostType';
    $wp_post_types['myCustomPostType']->rest_controller_class = 'WP_REST_Posts_Controller';
}

现在您应该有以下端点进行查询:

/wp-json/wp/v2/myCustomPostType

myCustomPostType为您注册的自定义帖子类型。"rest_base"不必匹配自定义帖子类型的名称。

您很可能想要添加特定于自定义帖子类型的附加字段,例如帖子元数据或Advanced custom fields插件。对于这些场景,您可以通过在functions.php文件中添加如下代码片段来包含这些属性:

function add_myCustomPostType_fields_url_to_myCustomPostType_request( $data, $post, $request ) {
    $_data = $data->data;
    $customImageProperty = get_field('customImageProperty'); 
    $_data['customImageProperty'] = $customImageProperty['url'];
    $data->data = $_data;
    return $data;
}
add_filter( 'rest_prepare_myCustomPostType', 'add_myCustomPostType_fields_url_to_myCustomPostType_request', 10, 3 );

register_post_type(' post type的名称'…)而不是'add_new'的名称。将文章类型的名称更改为Magazine并签出结果。

通过恢复到v1的REST API插件和/wp-json/posts?type=name-of-post-type,我设法从该特定的帖子类型检索帖子。