如何使用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' );
我们对此表示任何帮助。