Skip to main content
edited body
Source Link
Johansson
  • 15.4k
  • 11
  • 43
  • 79

You can set your own keys in the JSON response array. It can be anything. Take a look at this simple example:

add_action( 'rest_api_init', function () {
    register_rest_route( 'shubham', '/get_the_post/', array(
            'methods' => 'GET', 
            'callback' => 'get_posts_by_rest' 
    ) );
});
// Callback function
function get_posts_by_rest( ){
    // Get the post id from the URL and validate it
    if( isset( $_REQUEST['post_id'] ) && filter_var( $_REQUEST['post_id'] , FILTER_VALIDATE_INT ) ) {
        $id = abs( $_REQUEST['post_id'] );
    } else {
        return __('Please enter a post ID.','text-domain');
    }
    // Fetch the post
    $post = get_post( $id );
    // Check if the post exists
    if( $post ) {
        // Now, form our own array
        $data = array();
        $data['id'] = $post->ID;
        $data['date'] = $post->post_date;
        $data['date_gmt'] = $post->post_date_gmt;
        $data['guid'] = $post->guid;
        $data['modified'] = $post->post_modified 
        $data['modified_gmt'] = $post->post_modified_gmt >post_modified_gmt;
        $data['slug'] = $post->post_name;
        $data['status'] = $post->post_status;
        $data['title'] = $post->post_title;
        $data['content'] = $post->post_content;
        $data['excerpt'] = $post->post_excerpt;
        // Add the rest of your content
        
        // Return the response
        return $data;
    } else {
        return __('Invalid post ID','text-domain');
    }
}

Now by visiting /wp-json/shubham/get_the_post?post_id=123 you will see the same structure as the default wp-json/wp/v2/posts for the post that has an ID of 123.

You can set your own keys in the JSON response array. It can be anything. Take a look at this simple example:

add_action( 'rest_api_init', function () {
    register_rest_route( 'shubham', '/get_the_post/', array(
            'methods' => 'GET', 
            'callback' => 'get_posts_by_rest' 
    ) );
});
// Callback function
function get_posts_by_rest( ){
    // Get the post id from the URL and validate it
    if( isset( $_REQUEST['post_id'] ) && filter_var( $_REQUEST['post_id'] , FILTER_VALIDATE_INT ) ) {
        $id = abs( $_REQUEST['post_id'] );
    } else {
        return __('Please enter a post ID.','text-domain');
    }
    // Fetch the post
    $post = get_post( $id );
    // Check if the post exists
    if( $post ) {
        // Now, form our own array
        $data = array();
        $data['id'] = $post->ID;
        $data['date'] = $post->post_date;
        $data['date_gmt'] = $post->post_date_gmt;
        $data['guid'] = $post->guid;
        $data['modified'] = $post->post_modified 
        $data['modified_gmt'] = $post->post_modified_gmt 
        $data['slug'] = $post->post_name;
        $data['status'] = $post->post_status;
        $data['title'] = $post->post_title;
        $data['content'] = $post->post_content;
        $data['excerpt'] = $post->post_excerpt;
        // Add the rest of your content
        
        // Return the response
        return $data;
    } else {
        return __('Invalid post ID','text-domain');
    }
}

Now by visiting /wp-json/shubham/get_the_post?post_id=123 you will see the same structure as the default wp-json/wp/v2/posts for the post that has an ID of 123.

You can set your own keys in the JSON response array. It can be anything. Take a look at this simple example:

add_action( 'rest_api_init', function () {
    register_rest_route( 'shubham', '/get_the_post/', array(
            'methods' => 'GET', 
            'callback' => 'get_posts_by_rest' 
    ) );
});
// Callback function
function get_posts_by_rest( ){
    // Get the post id from the URL and validate it
    if( isset( $_REQUEST['post_id'] ) && filter_var( $_REQUEST['post_id'] , FILTER_VALIDATE_INT ) ) {
        $id = abs( $_REQUEST['post_id'] );
    } else {
        return __('Please enter a post ID.','text-domain');
    }
    // Fetch the post
    $post = get_post( $id );
    // Check if the post exists
    if( $post ) {
        // Now, form our own array
        $data = array();
        $data['id'] = $post->ID;
        $data['date'] = $post->post_date;
        $data['date_gmt'] = $post->post_date_gmt;
        $data['guid'] = $post->guid;
        $data['modified'] = $post->post_modified 
        $data['modified_gmt'] = $post->post_modified_gmt;
        $data['slug'] = $post->post_name;
        $data['status'] = $post->post_status;
        $data['title'] = $post->post_title;
        $data['content'] = $post->post_content;
        $data['excerpt'] = $post->post_excerpt;
        // Add the rest of your content
        
        // Return the response
        return $data;
    } else {
        return __('Invalid post ID','text-domain');
    }
}

Now by visiting /wp-json/shubham/get_the_post?post_id=123 you will see the same structure as the default wp-json/wp/v2/posts for the post that has an ID of 123.

added 239 characters in body
Source Link
Johansson
  • 15.4k
  • 11
  • 43
  • 79

You can set your own keys in the JSON response array. It can be anything. Take a look at this simple example:

add_action( 'rest_api_init', function () {
    register_rest_route( 'shubham', '/get_the_post/', array(
            'methods' => 'GET', 
            'callback' => 'get_posts_by_rest' 
    ) );
});
// Callback function
function get_posts_by_rest( ){
    // Get the post id from the URL and validate it
    if( isset( $_REQUEST['post_id'] ) && filter_var( $_REQUEST['post_id'] , FILTER_VALIDATE_INT ) ) {
        $id = $_REQUEST['post_id'];abs( $_REQUEST['post_id'] );
    } else {
        return __('Please enter a post ID.','text-domain');
    }
    // Fetch the post
    $post = get_post( $id );
    // Check if the post exists
    if( $post ) {
        // Now, form our own array
        $data = array();
        $data['id'] = $post->ID;
        $data['date'] = $post->post_date;
        $data['date_gmt'] = $post->post_date_gmt;
        $data['guid'] = $post->guid;
        $data['modified'] = $post->post_modified 
        $data['modified_gmt'] = $post->post_modified_gmt 
        $data['slug'] = $post->post_name;
        $data['status'] = $post->post_status;
        $data['title'] = $post->post_title;
        $data['content'] = $post->post_content;
        $data['excerpt'] = $post->post_excerpt;
        // Add the rest of your content
        
        // Return the response
        return $data;
    } else {
        return __('Invalid post ID','text-domain');
    }
}

Now by visiting /wp-json/shubham/get_the_post?post_id=123 you will see the same structure as the default wp-json/wp/v2/posts for the post that has an ID of 123.

You can set your own keys in the JSON response array. It can be anything. Take a look at this simple example:

add_action( 'rest_api_init', function () {
    register_rest_route( 'shubham', '/get_the_post/', array(
            'methods' => 'GET', 
            'callback' => 'get_posts_by_rest' 
    ) );
});
// Callback function
function get_posts_by_rest(){
    // Get the post id from the URL
    $id = $_REQUEST['post_id'];
    // Fetch the post
    $post = get_post( $id );
    // Now, form our own array
    $data = array();
    $data['id'] = $post->ID;
    $data['date'] = $post->post_date;
    $data['date_gmt'] = $post->post_date_gmt;
    $data['guid'] = $post->guid;
    $data['modified'] = $post->post_modified 
    $data['modified_gmt'] = $post->post_modified_gmt 
    $data['slug'] = $post->post_name;
    $data['status'] = $post->post_status;
    $data['title'] = $post->post_title;
    $data['content'] = $post->post_content;
    $data['excerpt'] = $post->post_excerpt;
    // Add the rest of your content
    
    // Return the response
    return $data;
}

Now by visiting /wp-json/shubham/get_the_post?post_id=123 you will see the same structure as the default wp-json/wp/v2/posts for the post that has an ID of 123.

You can set your own keys in the JSON response array. It can be anything. Take a look at this simple example:

add_action( 'rest_api_init', function () {
    register_rest_route( 'shubham', '/get_the_post/', array(
            'methods' => 'GET', 
            'callback' => 'get_posts_by_rest' 
    ) );
});
// Callback function
function get_posts_by_rest( ){
    // Get the post id from the URL and validate it
    if( isset( $_REQUEST['post_id'] ) && filter_var( $_REQUEST['post_id'] , FILTER_VALIDATE_INT ) ) {
        $id = abs( $_REQUEST['post_id'] );
    } else {
        return __('Please enter a post ID.','text-domain');
    }
    // Fetch the post
    $post = get_post( $id );
    // Check if the post exists
    if( $post ) {
        // Now, form our own array
        $data = array();
        $data['id'] = $post->ID;
        $data['date'] = $post->post_date;
        $data['date_gmt'] = $post->post_date_gmt;
        $data['guid'] = $post->guid;
        $data['modified'] = $post->post_modified 
        $data['modified_gmt'] = $post->post_modified_gmt 
        $data['slug'] = $post->post_name;
        $data['status'] = $post->post_status;
        $data['title'] = $post->post_title;
        $data['content'] = $post->post_content;
        $data['excerpt'] = $post->post_excerpt;
        // Add the rest of your content
        
        // Return the response
        return $data;
    } else {
        return __('Invalid post ID','text-domain');
    }
}

Now by visiting /wp-json/shubham/get_the_post?post_id=123 you will see the same structure as the default wp-json/wp/v2/posts for the post that has an ID of 123.

Source Link
Johansson
  • 15.4k
  • 11
  • 43
  • 79

You can set your own keys in the JSON response array. It can be anything. Take a look at this simple example:

add_action( 'rest_api_init', function () {
    register_rest_route( 'shubham', '/get_the_post/', array(
            'methods' => 'GET', 
            'callback' => 'get_posts_by_rest' 
    ) );
});
// Callback function
function get_posts_by_rest(){
    // Get the post id from the URL
    $id = $_REQUEST['post_id'];
    // Fetch the post
    $post = get_post( $id );
    // Now, form our own array
    $data = array();
    $data['id'] = $post->ID;
    $data['date'] = $post->post_date;
    $data['date_gmt'] = $post->post_date_gmt;
    $data['guid'] = $post->guid;
    $data['modified'] = $post->post_modified 
    $data['modified_gmt'] = $post->post_modified_gmt 
    $data['slug'] = $post->post_name;
    $data['status'] = $post->post_status;
    $data['title'] = $post->post_title;
    $data['content'] = $post->post_content;
    $data['excerpt'] = $post->post_excerpt;
    // Add the rest of your content
    
    // Return the response
    return $data;
}

Now by visiting /wp-json/shubham/get_the_post?post_id=123 you will see the same structure as the default wp-json/wp/v2/posts for the post that has an ID of 123.