Support

Account

Home Forums Backend Issues (wp-admin) ACF load_value to Gallery Field Reply To: ACF load_value to Gallery Field

  • Hi John,

    So there’s no more warning, and if I print_r on attachments I get:

    
    Array ( [0] => Array ( [title] => Image 0 [image] => Array ( [attachment_id] => 1653 ) ) [1] => Array ( [title] => Image 1 [image] => Array ( [attachment_id] => 1652 ) ) [2] => Array ( [title] => Image 2 [image] => Array ( [attachment_id] => 1651 ) ) [3] => Array ( [title] => Image 3 [image] => Array ( [attachment_id] => 1650 ) ) [4] => Array ( [title] => Image 4 [image] => Array ( [attachment_id] => 1649 ) ) [5] => Array ( [title] => Image 5 [image] => Array ( [attachment_id] => 1648 ) ) )
    

    It looks like the correct data is being returned but there are still no images loading in gallery field. Just checking that I’ve understood you correctly, this is the ammended snippet:

    
    function build_gallery_from_rs($value, $post_id, $field) {
      if (!empty($value)) {
        // already has a value
        return $value;
      }
      // does not have a value, build value from slider attachments
      global $wpdb;
      $table = $wpdb->prefix . 'new_royalsliders';
      $id = get_field('media_gallery_slider', $post_id, false);
      $slider_data = $wpdb->get_results( $wpdb->prepare("SELECT * FROM $table WHERE id=%d", $id), ARRAY_A );
      
      // Build image array
      //$value = array();
      if ($slider_data) {
    
        foreach ($slider_data as $slide) {
          $attachments = json_decode($slide['slides'], true);
          //print_r($attachments);
          if ($attachments) {
            foreach ($attachments as $attachment) {
              $value = $attachment['attachment_id'];
            }
          }
        }
    
      }
      return($value);
      
    }
    

    Many thanks again!