airesvsg / acf-to-rest-api

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

Get Image ACF fields on gallery on page #220

Open wirsing opened 6 years ago

wirsing commented 6 years ago

@airesvsg I am trying to use get fields filter on pages to retrieve custom fields of gallery images:

add_filter( 'acf/rest_api/page/get_fields', function( $data, $request, $response ) {
  if ( $response instanceof WP_REST_Response ) {
    $data = $response->get_data();
  }

  if ( ! empty( $data ) && isset( $data['acf']['winner_gallery'] ) ) {
    foreach ( $data['acf']['gallery'] as &$rel ) {
      rest_api_format_images( $rel );
    }
  }

  return $data;
}, 10, 3 );

function rest_api_format_images( &$object ) {
  if ( isset( $object->post_type ) ) {
    $post_type_object =  get_post_type_object( $object->post_type );
    $request = new WP_REST_Request(
      'GET',
      sprintf( '/wp/v2/%s/%d', ! empty( $post_type_object->rest_base ) ? $post_type_object->rest_base : $post_type_object->name, $object->ID)
    );

    $response = rest_do_request( $request );

    if ( ! $response->is_error() ) {
      $object = $response->get_data();
    }
  }
}

It doesn't seem to pull the fields, am I using the right {type} for the filter?

wirsing commented 6 years ago

hi @airesvsg just bumping this, can't seem to get it working. any leads or things I am missing? Thanks

steffanhalv commented 6 years ago

Im waiting for this to ..

navneil-naicker commented 6 years ago

At the time of this comment ACF Photo Gallery v1.6.3 is still under development. You can download the beta version of the source code from here.

Replace the current version of the code on your WordPress website plugin folder name 'navz-photo-gallery' with the beta version. ACF Photo Gallery v1.6.3 has support for ACF to REST API.

Usage 1 http://www.example.com/wp-json/acf/v3/pages/<POST_ID>/<Field_Name>?type=photo_gallery

Example http://www.example.com/wp-json/acf/v3/pages/2/vacation_photos?type=photo_gallery

Usage 2 http://www.example.com/wp-json/acf/v3/pages/<POST_ID>/<Field_Name>?type=photo_gallery&order=ASC&orderby=post__in

Example http://www.example.com/wp-json/acf/v3/pages/2/vacation_photos?type=photo_gallery&order=ASC&orderby=post__in