• Resolved enriqued55

    (@enriqued55)


    The gallery appears in the edit page but I can’t see the gallery (url,ids,images) in the api, but I can see all the other acf data.

    • This topic was modified 3 years, 9 months ago by enriqued55.
    • This topic was modified 3 years, 9 months ago by enriqued55.
Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter enriqued55

    (@enriqued55)

    I solved it:

    <?php
    $post_type = “evento”;
    function my_rest_prepare_post($data, $post, $request) {
    $_data = $data->data;

    $_data[‘acf_photo_gallery’] = acf_photo_gallery(‘galeria_detalle’, $post->ID);
    $data->data = $_data;
    return $data;
    }
    add_filter(“rest_prepare_{$post_type}”, ‘my_rest_prepare_post’, 10, 3);

    ?>

    Thank you for resolving this, I found your answer very useful. However, the syntax you used threw errors when I tried to paste it directly into my site – specifically the quote marks and , it appears they are not valid for PHP. You should use " and ' instead.

    Here is a correctly formatted version:

    $post_type = "evento";
    
    function my_rest_prepare_post($data, $post, $request) {
      $_data = $data->data;
    
      $_data['acf_photo_gallery'] = acf_photo_gallery('galeria_detalle', $post->ID);
      $data->data = $_data;
    
      return $data;
    }
    
    add_filter("rest_prepare_{$post_type}", 'my_rest_prepare_post', 10, 3);

    Also, for anyone new to WordPress (like me), some tips on where/how to use this would have been great!

    The $post_type variable should be the slug of your post type, and the first argument passed to acf_photo_gallery should be the name of the acf gallery field you are trying to match. I eventually figured out I could install the Code Snippets plugin and add this as a new snippet. It now works fine.

    • This reply was modified 3 years, 6 months ago by lawrencewitt.
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘API REST’ is closed to new replies.