WP REST API가 게시 유형에서 게시물을 가져옵니다.
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를 사용하려면:
함수에서.테마의 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
myCustomPost등록한 사용자 지정 게시 유형을 입력합니다."rest_base"는 커스텀 투고 타입의 이름과 일치할 필요는 없습니다.
투고 메타데이터나 고급 커스텀 필드 플러그인 등 커스텀 투고 타입에 고유한 필드를 추가할 수 있습니다.이러한 시나리오에서는 이러한 속성을 함수에 추가하여 포함할 수 있습니다.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' 이름이 아닙니다.투고 타입의 이름을 Magazine으로 변경하고 결과를 체크합니다.도움이 됐으면 좋겠다.
REST API 플러그인의 v1로 되돌리고/wp-json/posts?type=name-of-post-type
나는 그 특정 유형의 투고에서 투고를 취득할 수 있었다.
언급URL : https://stackoverflow.com/questions/32393967/wp-rest-api-fetch-posts-from-post-type
'programing' 카테고리의 다른 글
ASP.NET MVC: 서버측 처리 후 성공 확인 메시지를 표시하는 방법 (0) | 2023.03.19 |
---|---|
woocommerce에서 물품의 재고 수량을 어떻게 얻습니까? (0) | 2023.03.19 |
Oracle 스토어드 프로시저에서 "현황"과 "현황"의 차이점은 무엇입니까? (0) | 2023.03.19 |
Spring MVC 애플리케이션에서 Swagger를 구현하는 '간단한' 방법 (0) | 2023.03.19 |
Flyway가 Spring Boot에서 작동하지 않을 때 디버깅하는 방법 (0) | 2023.03.19 |