Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Alexander Hepburn
Alexander Hepburn
14,613 Points

JSON API Only returning 20 posts

I have download JSON API on my wordpress website and have started coding my mobile app when I realized that the API was only returning 20 of the most recent posts. So my question is how do I get all of the posts not the recent 30 or 100 but all of the posts. I have tried yoururl.com/api/get_posts and yoururl.com/api/get_recent_posts but both say there are 22 posts but are only returning 20 posts.

3 Answers

Kevin Korte
Kevin Korte
28,149 Points

That's a really old plugin, hasn't been updated since 2013, so your problem could be simply that the plugin is not compatible with newer versions of WP. It's really hard to troubleshoot, because you'd have to untangle how the plugin author fetched posts, and find out if WP depreciated anything there. My first inclination is that maybe it was and issue with WP pagination settings, but I couldn't even look at the plugin's docs because they are no long online, the link just dies.

I'd probably just bag that plugin and use one more recently maintained. Here is one that was lasted updated about 45 days ago. I'd give that a shot.

https://wordpress.org/plugins/json-rest-api/

Alexander Hepburn
Alexander Hepburn
14,613 Points

Ok, I installed that but it still doesn't work when I go to yoururl.com/wp-json I get the same 20 posts not 21 posts.

Kevin Korte
Kevin Korte
28,149 Points

Are all posts published?

Kevin Korte
Kevin Korte
28,149 Points

Is this site live somewhere to look at from the front end?

Kevin Korte
Kevin Korte
28,149 Points

Can you send me the link here?

Kevin Korte
Kevin Korte
28,149 Points

Yeah, there is something weird going on with pagination, and I'm not sure how the two sticky posts are or are not interacting with the API, but if you add an extra filter to your API call, I think I got all of the posts to show.

I will say, if this list is going to grow, us Wordpress's Codex in the WP_Query section to filter your JSON response and paginate it. But as is it's not too bad.

Add this to your default URL

/wp-json/posts?filter[posts_per_page]=-1

Alexander Hepburn
Alexander Hepburn
14,613 Points

Thank you so much, do you by chance know how to retrieve just the posts count because in the other plugin I added a /api/get_number_of_posts and I got something that looked like this:

["count": "21"] is there a way to do that?