Sunday, November 6, 2016

WP API with ACF - a crash course / quick tutorial

WP Restful API is frequently implemented with help of the following WP Plugin:


Support for fetching ACF fields tied to the posts is added via this WP Plugin:

Once you have these two plugins activated, you should be able to use these URLs to fetch posts via a RESTful API:

This will return first 10 posts from your site. The total number of records will be appended to HTTP Response Headers of the REST API call. 

In case you would like to obtain ACF fields tied to a post (i.e. post_id = 892), you can issue a RESTFul GET call like this:

It will return full list of ACF fields tied to the post with ID = 892. 

Also , there is an option  to extend the response by adding a custom field like this:

In the example above, we add a new field to JSON output - acf1. The field acf1 will contain everything that can be obtained by get_fields(ID). So, the next time you issue a RESTful call like this:

your JSON result will contain basic fields of the posts , like id, date ,etc. However, every record will contain the acf1 field , too!


By default, WP-API returns 10 records per page. In caase you would like it to return all records, you should set per_page parameter to -1, like in the following code:

Note that the filter is CPT dependent. This filter will be only executed on market CPT.

No comments:

Post a Comment

Ubuntu 12.04, 14.04, 16.04 - auto start an app or script before login

To run a command or application at startup, even before the user has logged in, you can use this file: /etc/rc.local The commands entered...