WP JSON Posts

Posted 11.10.31 @ 20:58 | Last updated 12.01.05
Wordpress Project

Have you ever wanted to automagically load new post to the bottom of your front page like Twitter do (you might want to look at WP More Posts)? Maybe you want to load some pages with AJAX and display them in a fancy lightbox like I do (try to click on about or contact!)? What if I say it’s all possible in WordPress and it’s really simple?

What WP JSON Posts does is returning any page, post or feed you like as JSON. If you want to load a page into a lightbox you shouldn’t need to use something else than the actual URL. And that’s exactly how easy WP JSON Posts is to use. An AJAX request to http://example.com/this-is-my-page?jsonposts=1 will give you a JSON representation of that page.

I’m sure you already got some ideas about how you can implement this in your own theme or plugin. Go ahead and download WP JSON Posts now! Otherwise just read on and I’ll show you just how deep the rabbit… how much you can accomplish with this WordPress plugin.

Feature overview:

Note: custom queries, page templates and other stuff you put in the template files are ignored. That’s kinda the definition of this plugin.

What exactly does it do?

The strength of WP JSON Posts is that it’s based on WordPress own built in post query. That is, when you visit a certain URL WordPress does some hard work figuring out what you want to see and goes to get those posts. Usually WordPress then applies the correct template, be it single.php or taxonomy-whatever.php. But if WP JSON Posts finds a key named ‘jsonposts’, the theme is swapped for JSON.

As of version 1.0.1 the following data can be returned for each post:

There’s one more thing…

Well, four actually. These are the parameters you can send as GET requests to customize what post data that are returned. In summary you could send &exclude=the_content-the_category&number_of_posts=10&offset=10&imgsize=small.

The above data contains pretty much a shitload of data which might be unnecessary. Often you only want the title and the featured image. Thats where the &exclude parameter comes in handy. My plugin expects it to be a dash-separated string of properties to remove. For example &exclude=the_content-the_date-the_category would remove those properties from the returned post objects. Default is to send everything.

Sometimes you don’t want to get the full normal result of a feed, but only the first couple of posts. For example on the front page of my portfolio I want to get a different number of posts depending on how long the rows are. &number_of_posts=5 will take care of this.

The third parameter you may attach is &offset. This is used to specify how many post you’ve already got, just like WordPress’ own offset parameter. &offset=10 would start getting posts at the eleventh post. Default is 0.

Finally you might have to specify what size you want the featured image to be. &imgsize=case would return the image size named “case”. Default is “small”.

By now I’m really sure you’ve got a few ideas how to use this plugin. Why don’t you download WP JSON Posts now? And don’t hesitate to write some feedback in the comment section below.

Comments

1 comment

  • WP More Posts - Andreas Hultgren

    [...] scroll to the bottom? This is exactly what WP More Posts does. Utilizing the API-like interface of WP JSON Posts, posts are fetched and included on the page you’re already [...]

  • Tell me what you think