Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wordpress API json, how to get prev and next posts in single post?

how to get prev and next posts in single post wordpress api, i can't get this, how to make json prev and next like in wordpress without API i want to get slug posts i can use next prev in single post, how to get slug, link, or id post for next and prev

<?php
$prev_post = get_previous_post();
if (!empty( $prev_post )): ?>
  <a href="<?php echo $prev_post->guid ?>"><?php echo $prev_post->post_title ?></a>
<?php endif ?>

like this but use in json https://codex.wordpress.org/Function_Reference/previous_post_link

like image 833
Ikhsan Mahendri Avatar asked Mar 02 '17 04:03

Ikhsan Mahendri


People also ask

How do I get posts from WordPress API?

To use the WordPress REST API, simply add /wp-json/wp/v2/posts to the end of your WordPress site URL. This will give you a list of posts (in JSON format). The default number of posts returned is 10, but you can choose to show more or less with the per_page argument — we'll talk about that below.

How do I use WordPress WP JSON?

Accessing all of your site data via the REST API is as simple as composing a URL. For any WordPress site running at least version 4.7, add the following string to the end of your site's url: /wp-json/wp/v2 (e.g., http://example.com/wp-json/wp/v2 ). Put that URL in your browser, and see what comes up.

How do I post to REST API?

To post JSON to a REST API endpoint, you must send an HTTP POST request to the REST API server and provide JSON data in the body of the POST message. You also need to specify the data type in the body of the POST message using the Content-Type: application/json request header.


1 Answers

A little late to the party – but here is an answer.

While I agree with @Chris answer that a REST API is different from the PHP API available for themes, sometimes we still build the same frontends from the data, right?

If I want to show links on my blog to the next and previous post, I don't want to send 3 requests to the API.

As a solution I included this in my plugin containing the API adjustments for the specific project:

// Add filter to respond with next and previous post in post response.
add_filter( 'rest_prepare_post', function( $response, $post, $request ) {
  // Only do this for single post requests.
  if( $request->get_param('per_page') === 1 ) {
        global $post;
        // Get the so-called next post.
        $next = get_adjacent_post( false, '', false );
        // Get the so-called previous post.
        $previous = get_adjacent_post( false, '', true );
        // Format them a bit and only send id and slug (or null, if there is no next/previous post).
        $response->data['next'] = ( is_a( $next, 'WP_Post') ) ? array( "id" => $next->ID, "slug" => $next->post_name ) : null;
        $response->data['previous'] = ( is_a( $previous, 'WP_Post') ) ? array( "id" => $previous->ID, "slug" => $previous->post_name ) : null;
  }

    return $response;
}, 10, 3 );

Which gives you something like this:

[
  {
    "ID": 123,
    ...
    "next": {
      "id": 212,
      "slug": "ea-quia-fuga-sit-blanditiis"
    },
    "previous": {
      "id": 171,
      "slug": "blanditiis-sed-id-assumenda"
    },
    ...
  }
]
like image 65
Thomas Ebert Avatar answered Oct 13 '22 22:10

Thomas Ebert