Join Morten Rand-Hendriksen for an in-depth discussion in this video Get the previous post ID and title, part of WordPress: REST API.
- [Instructor] A quick look at the API output for a single post tells us that although we have lots of information about the post itself, we have no information about the previous or next post. So, we need to figure out a way of getting that info. The good news is, WordPress pretty much has functions and template tags for everything we need and in this case, we have a function called get previous post that does exactly what it sounds like. It gets the previous post from the one we are currently at. So, in theory, we should be able to use this function to get the post ID, the title, and the link to the previous post and then add it to our rest API response.
Here I want to show you a quick and dirty method for testing whether or not the functions you find are actually giving you what you need. So, I want to see the actual response from get previous post, and I do that by first going into 2017 and finding the file single dot PHP and copy that file. Paste it into my 2017 child theme. Here it is. And here, inside the current post loop, when I have the current post object, I want to run get previous post and see the entire output of whatever that returns.
So, I'll say var, dump, then put in get previous post, that's the function. And then I'll just terminate everything right after that. I'll say die. Now, of course this will break the page, and make everything look like a big mess, and that's fine, because I'm working on my local computer. I'm just testing to see what is happening. Now, I can go to my site and go to a single post. And when I scroll down, I get the full var dump for that function. And here you see we have a new object for the pulse with a ID 3526. When I scroll down a bit you'll see here we have the post dates and the content and everything else that you get in the API response, except this time, I'm requesting it directly from WordPress.
So that means, if I want to ID, I should be able to point at ID. If I want to post title, I should be able to point at post title, and so on. So, let's try that. Let's change the call here to say I only want ID. Save it, and reload the single page. Now, I get a single line, and it just has to ID, 3526. I can do the same with the pulse title, then I first have to find out what that property is. Here you see pulse title, it's just post underscore title.
Save that. Reload the page. And here we have the post title. So that means I can use this function, get previous post, to get the ID and the post title. But, I also need the direct link to the post, and for that, I need to use an additional function called get permalink. So, I'll make a small change here. I'll set up a variable, call it previous post, and set get previous post as that variable.
Then I'll echo out the content of get permalink, which will give us the permalink for any post when we have the post ID, and the post ID will be previous post ID. Save that, reload the page, and here we have the permalink to the previous post, right down here at the bottom. So that means, using get previous post and get permalink, we'll be able to obtain all the information we need about the previous post.
The next step is to get that information into the rest API response, and that's what we'll do in the next movie.
- Using a REST client to communicate with a REST API
- Reviewing available routes, endpoints, and arguments
- Adding custom post types and taxonomies
- Altering the API response
- Using PHP for resiliency
- Passing info using wp_localize_script()
- Setting up a REST API request through AJAX
- Outputting selected post data to the front end
- Formatting post data to match a theme
- Triggering AJAX for an infinite scroll effect
- Using AJAX to load subsequent responses