Skip to content
This repository has been archived by the owner on Sep 24, 2018. It is now read-only.
This repository has been archived by the owner on Sep 24, 2018. It is now read-only.

Include next/previous links in single post responses #783

Closed
jakejackson1 opened this issue Jan 16, 2015 · 13 comments
Closed

Include next/previous links in single post responses #783

jakejackson1 opened this issue Jan 16, 2015 · 13 comments

Comments

@jakejackson1
Copy link

This is a feature request.

It would be good when using the /posts/:id endpoint if the next and previous post IDs where returned with the JSON data. It would make it easier to implement a 'next/previous' button in an app without making any extra calls.

@danielbachhuber
Copy link
Member

Interesting request. I'm not 100% on the best way to accommodate off the top of my head, but we'll keep it in the backlog of potential enhancements.

@goblindegook
Copy link

Including previous and next links in the href object of the response is where it'd probably make the most sense. But even if the default API ends up not implementing this, it's easily added using the json_prepare_post filter.

@jakejackson1
Copy link
Author

Thanks. I'll take a look at the json_prepare_post filter.

@rachelbaker
Copy link
Member

+1 for the idea of adding "prev" and "next" objects to _links for the single post responses.

@rachelbaker rachelbaker added this to the 2.0 milestone Feb 6, 2015
@rmccue rmccue changed the title /posts/:id - include next/previous post IDs Include next/previous links in single post responses Feb 11, 2015
@colinhowells
Copy link

I know that using json_prepare_post for this should be trivial, but I'm having a heck of a time getting it to go. It's as if the filter is using $post rather than the actual post being prepared, so it only returns the previous link of the most recent post.

function get_pagination_in_json( $post_response, $post, $context ) {

    $post = get_post($post['ID']);

    $previous_post = get_adjacent_post( true, '', true, 'my_custom_taxonomy_name' );
    $next_post = get_adjacent_post( true, '', false, 'my_custom_taxonomy_name' );

    if ( is_a( $previous_post, 'WP_Post' ) ) {
        $previous = get_permalink($previous_post->ID);
        $post_response['pagination']['previous'] = $previous;
    }

    if ( is_a( $next_post, 'WP_Post' ) ) {
        $next = get_permalink($next_post->ID);
        $post_response['pagination']['next'] = $next;
    }

    return $post_response;
}
add_filter( 'json_prepare_post', 'get_pagination_in_json', 10, 3 );

@petitphp
Copy link

The problem is that get_adjacent_post use the $GLOBALS['post'] to do its magic. And I don't think get_post ever change the global object.

One way of solving this is by temporarily overriding the global post.

function get_pagination_in_json( $post_response, $post, $context ) {

    // Ensure global post is correctly set
    $old_post = $GLOBALS['post'];
    $GLOBALS['post'] = (object)$post;

    $previous_post = get_adjacent_post( true, '', true, 'my_custom_taxonomy_name' );
    $next_post = get_adjacent_post( true, '', false, 'my_custom_taxonomy_name' );

    if ( is_a( $previous_post, 'WP_Post' ) ) {
        $previous = get_permalink($previous_post->ID);
        $post_response['pagination']['previous'] = $previous;
    }

    if ( is_a( $next_post, 'WP_Post' ) ) {
        $next = get_permalink($next_post->ID);
        $post_response['pagination']['next'] = $next;
    }

    // Reset global post to its old value
    $GLOBALS['post'] = $old_post;

    return $post_response;
}
add_filter( 'json_prepare_post', 'get_pagination_in_json', 10, 3 );

@colinhowells
Copy link

I knew getting around the global post was how to do it, just couldn't figure out how ... you are a scholar and a gentleman, many thanks for your help!

This is just a temporary workaround until the 2.0 milestone but since I'm not sure how long that'll be, this is a huge help for now.

@putuyoga
Copy link

well, i don't know how to using 'load more' or lazy load posts for my mobile app using this api, because example using page=2 query var in /wp-json/posts will return exist post if my blog update post like every 5 minutes.

maybe any hint ?

@colinhowells
Copy link

If I understand you right and you're saying you add so many posts so quickly that page 1 would quickly become page 2, I think that would be a problem whether you were using the API or not ;) (If you add your post IDs to a do_not_duplicate array, you can work that out, though.)

Setting that aside for the moment, the API is perfect for doing lazy load of pages.

  • Run a query (say posts_per_page=10 or whatever)
  • Set a counter = 1;
  • Get the posts back and display them
  • If user scrolls to end or hits a button, set counter = 2 (counter++)
  • Run query, but: page=counter (or use offset, rather than page number)
  • Get the posts back ...
  • Repeat

Spend some time with https://github.com/scottopolis/wp-rest-api-demo/blob/master/js/app.js which is for an app, but it's pretty simple and outlines one method of doing this ...

@denitto
Copy link

denitto commented Apr 29, 2015

+1. @rachelbaker: will this also work for a request that uses the format: filter[name]=post_name?

@danielbachhuber
Copy link
Member

Would next be the next post in the post type? How do we only enable the functionality for post types that are expected to have pagination?

@danielbachhuber danielbachhuber removed this from the 2.0 milestone Dec 9, 2015
@danielbachhuber
Copy link
Member

We aren't going to add this to the Posts controller for the foreseeable future (discussion):

rmccue [3:22 PM] We can do it naively, which is our best option
​[3:22] i.e. adjacent_post_link
​[3:23] If you want to paginate through a custom query, you're SOL
danielbachhuber [3:24 PM] hm. I'm not convinced it's something we should add in core
rmccue [3:24 PM] I get the use case, but probably better to leave it for plugin land
​[3:24] Alternatively, you can just get the collection and go through that instead
​[3:24] Probably a better way

@te-online
Copy link

I found this issue via search engine and wanted to point to a Stack Overflow answer, where I provided a naive, but simple filter function for this feature – just in case somebody needs it for a project:
https://stackoverflow.com/questions/42546957/wordpress-api-json-how-to-get-prev-and-next-posts-in-single-post/48289814

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

9 participants