wp rest api fetch all custom post types in one request code example
Example: wp rest api fetch all custom post types in one request
add_action( 'rest_api_init', 'custom_api_get_all_posts' );
function custom_api_get_all_posts() {
register_rest_route( 'custom/v1', '/all-posts', array(
'methods' => 'GET',
'callback' => 'custom_api_get_all_posts_callback'
));
}
function custom_api_get_all_posts_callback( $request ) {
$posts_data = array();
$paged = $request->get_param( 'page' );
$paged = ( isset( $paged ) || ! ( empty( $paged ) ) ) ? $paged : 1;
$posts = get_posts( array(
'paged' => $paged,
'post__not_in' => get_option( 'sticky_posts' ),
'posts_per_page' => 10,
'post_type' => array( 'post', 'books', 'movies' )
)
);
foreach( $posts as $post ) {
$id = $post->ID;
$post_thumbnail = ( has_post_thumbnail( $id ) ) ? get_the_post_thumbnail_url( $id ) : null;
$posts_data[] = (object) array(
'id' => $id,
'slug' => $post->post_name,
'type' => $post->post_type,
'title' => $post->post_title,
'featured_img_src' => $post_thumbnail
);
}
return $posts_data;
}