0

I am using below code to get the latest post from a category

www.mysite.com/wp-json/mynamespace/v1/latest-posts?category=it

but an error is showing

enter image description here

<?php 

        /**
         *
         * Get The latest post from a category !
         * @param array $params Options for the function.
           * @return string|null Post title for the latest,? * or null if none
         *
         */


         function get_latest_post ( $params ){
            $post = get_posts( array(
              'category'      => $category,
                'posts_per_page'  => 1,
                'offset'      => 0
          ) );

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

            return $post[0]->post_title;
         }

         // Register the rest route here.

         add_action( 'rest_api_init', function () {

                register_rest_route( 'mynamespace/v1', 'latest-post',array(

                    'methods'  => 'GET',
                    'callback' => 'get_latest_post'

                ) );

         } );

    ?>
0

First of all, you registered the route "latest-post", but the URL you used is "latest-posts".

Second: In your "get_latest_post" function, you use the variable $category, but it is set nowhere.

Fix the function like this:

function get_latest_post ( $params ){
       if(isset($params['category'])){
        $post = get_posts( array(
          'category'      => $params['category'],
            'posts_per_page'  => 1,
            'offset'      => 0
      ) );

        if( empty( $post ) ){
            return new WP_Error( 'no_post_found', 'there is no post in this category', array( 'status' => 404 ) );
        }

    $returnage = new WP_REST_Response( array('post_title' => $post[0]->post_title) );
    $returnage->set_status( 200 );
        return $returnage;
     } else { 
return new WP_Error( 'no_category_given', 'please add category parameter', array( 'status' => 404 ) );
  }
 } 

(The WP_Errors and WP_REST_Response ensure that you get a valid JSON REST API response from your function)

  • It's showing only one post, suppose today I added 5 posts in a particular category all posts should be show from that category. – Sumit Singh Sep 8 '17 at 7:30
  • in that case you need completely different get_posts arguments. Why use "posts_per_page" => 1 if you want all posts but from a specific date? Please get your requirements straight, which posts you need. – kuchenundkakao Sep 8 '17 at 7:34
  • Thank you for your valuable reply (Authors and Categories latest posts) I need to query from all authors that posted in the last 24 Hour and their latest post that are not older than 24 Hours via the REST API, I'll show Post Title, Post Content, Category name, featured images and all – Sumit Singh Sep 8 '17 at 10:42
  • I have changed 'posts_per_page' => 5, but still showing 1 – Sumit Singh Sep 8 '17 at 10:46
  • Of course it only shows 1. You return $post[0]->post_title, this means the FIRST post title. If you want to return all of the titles, you would need to do something like $posttitles = array(); foreach($post as $singlepost){ $posttitles[] = $singlepost->post_title; } $returnage = new WP_REST_Response( array('post_titles' => $posttitles) ); – kuchenundkakao Sep 8 '17 at 11:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.