endpoints - Get The latest post from a category

admin2025-01-07  5

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

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

but an error is showing

<?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'

                ) );

         } );

    ?>

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

<?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'

                ) );

         } );

    ?>
Share Improve this question asked Sep 8, 2017 at 6:05 Sumit SinghSumit Singh 115 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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)

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736252304a35.html

最新回复(0)