I am using the following plugins to get JSON data from WordPress REST API:
- Advanced Custom Fields PRO (5.3.7)
- WP REST API (2.0-beta13)
- ACF to REST API (2.2.0)
- Custom Post Type UI (1.3.4)
I have a custom post type “Case Study” created with the Custom Post Type UI plugin. I have a custom field group (through ACF) called “Case Study” that is only applied when the post type is Case Study.
I also have a custom field group called Home that is applied to the Home page. One of the fields is “Featured Case Study”. The field type is a Post Object and the “Filter by Post Type” is set to Case Study.
So now I edit my Home page and I am able to attach a Case Study object to the Home page using the Featured Case Study select box.
Via the WP REST API plugin I am able to get the Home page as JSON data and the ACF portion is provided via the ACF to REST API plugin. I can see all of the fields provided by ACF for the Home page.
The “Show in REST API” setting for the Case Study custom post type is also set to True, so the Case Study I selected for the Home page is also part of the JSON data.
So far, so good.
The problem is that the ACF data for the selected Case Study is not present in the Home page JSON data. The case study post object is displayed with the default WordPress fields, albeit, as JSON, but the ACF data is not shown.
I am able to get the selected Case Study by itself through the REST API and all desired fields are visible. It is only when this object is attached to the Home page that I am unable to see the ACF fields for it.
Is there any way to get the ACF data for a custom post type that is attached to another ACF field?
Figured it out from looking at this Github issue. I needed to add a special filter to functions.php in my theme. Non-intuitive and clunky, but it works.
I changed the arguments of get_fields in the last version ( v3 ), I removed the $response and $object, now we only have these arguments: $data and $request.
How to use:
The wildcard {type} can be: post | page | user | comment | attachment | custom post type | taxonomy