Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Roshanb54/0765e9be0f8585e11ebaa61a5c29ac2d to your computer and use it in GitHub Desktop.
Save Roshanb54/0765e9be0f8585e11ebaa61a5c29ac2d to your computer and use it in GitHub Desktop.
add_filter( 'rest_prepare_(post_type)', 'function_name', 10, 3 );
function function_name( $data, $post, $request ) {
$_data = $data->data;
$thumbnail_id = get_post_thumbnail_id( $post->ID );
$thumbnail_full = wp_get_attachment_image_src( $thumbnail_id, 'full' );
$thumbnail_thumb = wp_get_attachment_image_src( $thumbnail_id, 'thumbnail' );
$thumbnail_medium = wp_get_attachment_image_src( $thumbnail_id, 'medium' );
$thumbnail_large = wp_get_attachment_image_src( $thumbnail_id, 'large' );
if( ! empty($thumbnail_id) ) {
$_data['featured_image_thumbnail_url'] = array('full'=>$thumbnail_full[0], 'thumbnail' => $thumbnail_thumb[0], 'medium' => $thumbnail_medium[0], 'large' => $thumbnail_large[0] );
} else {
$_data['featured_image_thumbnail_url'] = null;
}
$post_meta = get_fields( $post->ID );
$_data['post_metas'] = $post_meta;
$data->data = $_data;
return $data;
}
add_action( 'rest_api_init', 'endpoint_function_name_here' );
function endpoint_function_name_here() {
register_rest_route( '/wp/v2', '/(custom slug)/', array(
'methods' => 'GET',
'callback' => 'custom_callback_function'
)
);
}
function custom_callback_function() {
$get_post_data = get_posts(array('post_type' => '(post_type)', 'posts_per_page' => -1));
$post_titles = wp_list_pluck( $get_post_data, 'post_title' );
return $post_titles;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment