Support

Account

Home Forums Add-ons Gallery Field Convert Attachments to Gallery Reply To: Convert Attachments to Gallery

  • You could create an acf/load_field filter https://www.advancedcustomfields.com/resources/acfload_field/ or an acf/load_value filter https://www.advancedcustomfields.com/resources/acfload_value/. You could then use the WP get_attached_media() function https://codex.wordpress.org/Function_Reference/get_attached_media to get all of the media for a post.

    
    add_filter('acf/load_value/name=your-field-name', 'build_gallery_from_attachments', 10, 3);
    function build_gallery_from_attachments($value, $post_id, $field) {
      if (!empty($value)) {
        // already has a value
        return;
      }
      // does not have a value, build value from attachments
      $media = get_attached_media('image', $post_id);
      $value = array();
      if ($media) {
        foreach ($media as $image) {
          $value[] = $image->ID;
        }
      }
      return $value;
    }