0
votes

TL;DR: How to choose every bit of information of the response of a WP REST API custom endpoint?

LONG VERSION

If I want to build a custom endpoint with the WP REST API - sending specific post data from different post types - following the example in the Handbook, I got this:

function custom_endpoint ( $data ) {
    $posts = get_posts( array(
        'numberposts'   => -1,
        'post_type'     => array('event', 'post'),
    ) );

    if ( empty( $posts ) ) {
        return null;
    }

    return $posts;
}


add_action( 'rest_api_init', function () {
    register_rest_route( 'wp/v1', '/custom-endpoint/', array(
        'methods' => 'GET',
        'callback' => 'custom_endpoint',
    ) );
} );

But the get_post() function doesn't return some piece of data that is very useful if you wish to display posts in your page(category id, featured image, for intance). So how can I build a custom endpoint that returns:

  • Post Title
  • Post Date
  • Post Author
  • Post Excerpt
  • Post Content
  • Post Featured Image (like Better Featured Images plugin)
  • Post Category
  • Post Type
  • Post Link
  • Other usfeul informations
2

2 Answers

1
votes

Based in the @fsn answer, I got the following idea: take the object that get_posts() returns and add new proporties to it, using others Wordpress functions.

function custom_endpoint ( $data ) {
$posts = get_posts( array(
    'numberposts'   => -1,
    //Here we can get more than one post type. Useful to a home page.
    'post_type'     => array('event', 'post'), 
) );


if ( empty( $posts ) ) {
    return null;
}


$args = array();    

foreach ( $posts as $post ) {

 //Get informations that is not avaible in get_post() function and store it in variables.
   $category = get_the_category( $post->ID );
   $img_thumb = get_the_post_thumbnail_url( $post->ID, 'thumbnail' );       // Thumbnail (default 150px x 150px max)
   $img_medium = get_the_post_thumbnail_url( $post->ID, 'medium' );          // Medium resolution (default 300px x 300px max)
   $img_large = get_the_post_thumbnail_url( $post->ID, 'large' );           // Large resolution (default 640px x 640px max)
   $img_full = get_the_post_thumbnail_url( $post->ID, 'full' );            // Full resolution (original size uploaded)

 //Adds the informations to the post object.
   $post->category = $category; 
   $post->img_tumb = $img_thumb; 
   $post->img_medium = $img_medium; 
   $post->img_large = $img_large; 
   $post->img_full = $img_full;

   array_push($args, $post);
   }
return $args;
}
add_action( 'rest_api_init', function () {
  register_rest_route( 'wp/v1', '/custom-endpoint/', array(
    'methods' => 'GET',
    'callback' => 'custom_endpoint',
) );

} );

It works fine!

Thanks, @fsn for the contribution.

0
votes

As the WP Codex states to access all data:

Access all post data Some post-related data is not available to get_posts.

You can get them by:

$posts = get_posts( array(
        'numberposts'   => -1,
        'post_type'     => array('event', 'post'),
    ) );

$response = [];
foreach ( $posts as $post ) {
  $response[] = [
    'content' => $post->CONTENT.
     'title' =>  $post->TITLE,
      .....
  ]
}

return $response; (in a WP way of constructing json responses)