簡體   English   中英

WP REST API從帖子類型中獲取帖子

[英]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') )
     )
);

要使用REST API插件的v2:

在主題的functions.php文件中,添加以下內容以創建休止端點:

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”不必與自定義帖子類型的名稱相匹配。

您很可能希望添加特定於自定義帖子類型的其他字段,例如帖子元數據或可能來自高級自定義字段插件。 對於這些場景,您可以通過向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('帖子類型名稱'...)不是'add_new'名稱。 將您的帖子類型的名稱更改為雜志並簽出結果。 希望能幫助到你。

通過恢復到REST API插件的v1並使用/wp-json/posts?type=name-of-post-type我設法從該特定帖子類型中檢索帖子。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM