0
votes

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?

3

3 Answers

0
votes

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.

add_filter( 'acf/rest_api/page/get_fields', function( $data, $request, $response ) {
    if ( $response instanceof WP_REST_Response ) {
        $data = $response->get_data();
    }
    if( isset($data['acf']) && isset($data['acf']['featured_work']) ) {
        $data['acf']['featured_work'] = get_fields($data['acf']['featured_work']);
    }
    return $data;
}, 10, 3);
0
votes
add_filter( 'json_prepare_post', function ($data, $post, $context) {
    $data['acf'] = get_fields($post['ID']);
    return $data;
}, 10, 3 );
0
votes

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.


    $data     mixed( array, WP_REST_Request )
    $request  mixed( WP_REST_Request, NULL )
    $response mixed( WP_REST_Response, NULL )
    $object   mixed ( WP_Post, WP_Term, WP_User, NULL )

How to use:

add_filter( 'acf/rest_api/{type}/get_fields', function( $data ) {
    if ( isset( $data['acf']['featured_work'] ) ) {
        $data['acf']['featured_work'] = get_fields( $data['acf']['featured_work'] );
    }

    return $data;
} );

The wildcard {type} can be: post | page | user | comment | attachment | custom post type | taxonomy