airesvsg / acf-to-rest-api

Exposes Advanced Custom Fields Endpoints in the WordPress REST API
https://wordpress.org/plugins/acf-to-rest-api/
1.33k stars 111 forks source link

ACF, Categories and Featured Image for a custom post / relationship on WP REST API #328

Open melilocacr opened 4 years ago

melilocacr commented 4 years ago

I'm building a Gatsby site that sources from a WP back end with GraphQL. I'm having issues preparing my data to send over through the API.

I have a custom post called case studies. These case studies have categories, a featured image and several ACF fields that I need to be able to access through REST. I've created a custom REST API endpoint for my case studies as such:

/************************************************
 * Case Study REST API Endpoint
 * *************************************************/ 
function caseStudyEndpoint( $request_data ) {

  // setup query argument
  $args = array(
      'post_type' => 'case-study',
      'posts_per_page' => -1
  );

  // get posts
  $posts = get_posts($args);

  // add custom field data to posts array 
  foreach ($posts as $key => $post) {
          $posts[$key]->acf = get_fields($post->ID);
          $posts[$key]->link = get_permalink($post->ID);
          $posts[$key]->image = get_the_post_thumbnail_url($post->ID);
          $posts[$key]->categories = wp_get_post_categories($post->ID);
  }
  return $posts;
}

// register the endpoint;
add_action( 'rest_api_init', function () {
  register_rest_route( 'case_study_endpoint/v2', '/case-studies/', array(
      'methods' => 'GET',
      'callback' => 'caseStudyEndpoint',
      )
  );
});

Through this endpoint I can easily access all the fields, including featured image, ACF and categories.

In addition, I have an ACF block called Related Case Studies, which has a number of relationship fields that link to case studies. From that block, I can also retrieve the ACF for the related case study, as well as the categories, image and such. I'm able to accomplish this thanks to this recursive snippet.

/**
* ACF, Image and Category Data in Post Object Response
*/

$types = ['post', 'page', 'case-study'];

foreach ( $types as $type ) {
    add_filter( 'acf/rest_api/'.$type.'/get_fields', function( $data, $response ) use ( $types ) {

        if ( $response instanceof WP_REST_Response ) {
            $data = $response->get_data();
        }

        array_walk_recursive( $data, 'deepIncludeFields', $types );

        return $data;

    }
    , 10, 3 );
}

function deepIncludeFields( &$item, $key, $postTypes ) {
    if ( isset( $item->post_type ) && in_array( $item->post_type, $postTypes ) ) {
        $item->acf = get_fields( $item->ID );
        $item->image = get_the_post_thumbnail_url($item->ID);
        $item->categories = wp_get_post_categories($item->ID, array( 'fields' => 'names' ));
    }
}

So I am covered in being able to retrieve all my data for a case study by accessing the Case Study endpoint and I'm also able to retrieve all the data recursively from a relationship field while using my regular WordpressPage endpoint.

However, within the case studies themselves I am also hoping to have the Related Case Studies block, and to be able to access the data for other case studies recursively from there as well. But this isn't working.

I know I'm very close, and the solution might be to use a different WP filter for my recursive ACF function, or something. Basically the ACF Recursion is missing within the Case Study Custom post endpoint.

Any thoughts?