2015-09-04 97 views
8

如何使用WP REST API(v1或v2)从特定自定义帖子类型获取所有帖子?我对此很陌生,并试图了解如何做到这一点。来自帖子类型的WP REST API提取帖子

我目前使用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'); 

任何帮助,这是非常感谢。

回答

5

没有为第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')) 
    ) 
); 
+0

我在哪里添加?谢谢。 – Si8

+0

@Si8在你的'functions.php'文件中;) –

+0

我只想包含帖子的ID,这就是全部。上述工作?如果有必要,我可以提出一个新问题。谢谢 – Si8

2

register_post_type('帖子类型的名称'...)而不是'add_new'的名称。 将您的帖子类型的名称更改为杂志并签出结果。希望能帮助到你。

+0

谢谢你,我做了改变,但遗憾的是它并没有解决我的问题。我恢复到了REST API插件的v1版本,并且http://domain.com/wp-json/posts?type=magazine我设法从该特定帖子类型中检索了帖子。谢谢 – Jeff

2

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

2

使用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); 
相关问题