2015-09-04 94 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); 
相關問題