WP REST API 从 post 类型中获取 posts
WP REST API fetch posts from post type
如何使用 WP REST API(v1 或 v2)从特定自定义 post 类型获取所有 post?我对此很陌生,正试图了解如何做到这一点。
我目前正在使用 WP REST API v2 并设法用这个
获取所有 post 类型的列表
http://domain.com/wp-json/wp/v2/types
然后设法得到我感兴趣的 post 类型
http://domain.com/wp-json/wp/v2/types/the-icons-update
如何从该特定内容类型中获取所有 post?
我试过
http://domain.com/wp-json/wp/v2/posts?filter[post_type]=the-icons-update
但它 returns 是一个空数组(我想它 returns 默认 posts 而在我的网站上,自定义 [=] 中只有 posts 34=] 我正在尝试检索的类型)。
我注册 post 类型的方式会不会有问题?
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' );
非常感谢任何帮助。
register_post_type('name of post type'...) 不是 'add_new' 名称。
将 post 类型的名称更改为杂志并检查结果。希望对你有帮助。
通过恢复到 REST API 插件的 v1 并使用 /wp-json/posts?type=name-of-post-type
,我设法从特定的 post 类型中检索了 posts。
要使用 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 是您注册的自定义 post 类型。 "rest_base" 不必与自定义 post 类型的名称匹配。
您很可能希望添加特定于您的自定义 post 类型的其他字段,例如 post 元数据或可能来自 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 );
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') )
)
);
如何使用 WP REST API(v1 或 v2)从特定自定义 post 类型获取所有 post?我对此很陌生,正试图了解如何做到这一点。
我目前正在使用 WP REST API v2 并设法用这个
获取所有 post 类型的列表http://domain.com/wp-json/wp/v2/types
然后设法得到我感兴趣的 post 类型
http://domain.com/wp-json/wp/v2/types/the-icons-update
如何从该特定内容类型中获取所有 post?
我试过
http://domain.com/wp-json/wp/v2/posts?filter[post_type]=the-icons-update
但它 returns 是一个空数组(我想它 returns 默认 posts 而在我的网站上,自定义 [=] 中只有 posts 34=] 我正在尝试检索的类型)。
我注册 post 类型的方式会不会有问题?
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' );
非常感谢任何帮助。
register_post_type('name of post type'...) 不是 'add_new' 名称。 将 post 类型的名称更改为杂志并检查结果。希望对你有帮助。
通过恢复到 REST API 插件的 v1 并使用 /wp-json/posts?type=name-of-post-type
,我设法从特定的 post 类型中检索了 posts。
要使用 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 是您注册的自定义 post 类型。 "rest_base" 不必与自定义 post 类型的名称匹配。
您很可能希望添加特定于您的自定义 post 类型的其他字段,例如 post 元数据或可能来自 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 );
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') )
)
);