Skip Navigation

[Resolved] Get the image ID of the repeatable image fields

This thread is resolved. Here is a description of the problem and solution.

Problem:

The issue here is that the user wanted to get the id's of the repeatable image fields.

Solution:

This can be done by using the code below.

  // Add Shortcode
function get_image_id( $atts ) {
 global $wpdb;
 
    // Attributes
    $atts = shortcode_atts(
        array(
            'field' => '',
            'id' => ''
        ),
        $atts
    );
 
    $image_urls = get_post_meta($atts['id'],$atts['field']);
    $id_list = array();
    foreach ($image_urls as $image_url) {
        if(!empty($image_url)){
                 $attachment = $wpdb->get_col($wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE guid='%s';", $image_url )); 
                 array_push($id_list, $attachment[0]);
             }
        }
         
 return implode(",", $id_list); 
  
}
add_shortcode( 'get_image_id', 'get_image_id' );

So how this actually works is that you need to call it like this [get_image_id id='[wpv-post-id]' field='wpcf-field-name']
You can try using this to pass the ids of the image into the fushion gallery shortcode.

This support ticket is created 5 years, 10 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Our next available supporter will start replying to tickets in about 0.42 hours from now. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 8 replies, has 3 voices.

Last updated by kristofG 5 years, 9 months ago.

Assisted by: Shane.

Author
Posts
#1189837

I know this question has been asked many times before and I have tried a few solutions found here in the forum, but most shortcodes output a comma separated list of image IDs. This used to work for me using the Avada gallery shortcode like this

add_shortcode('repeating-images-gallery', 'repeating_images_gallery_func');
function repeating_images_gallery_func($atts, $content) {
    global $post, $wpdb;
      
    $images = get_post_meta($post->ID, 'wpcf-image-gallery', false);
    $ids = array();
    foreach ($images as $image) {
        $attachment_id = $wpdb->get_var($wpdb->prepare(
            "SELECT ID FROM $wpdb->posts WHERE guid = %s",
        $image
        ));
        $ids[] = $attachment_id;
    }
    $out = do_shortcode('[fusion_gallery image_ids="' . implode(",", $ids) . '" columns="4" column_spacing="20"]');
    return $out;
}

We just had to populate the image_ids and that was it, easy. However, the Fusion Gallery has been reworked in Avada 5.8 and now the Gallery is built like this.

[fusion_gallery layout="" picture_size="" columns="" column_spacing="10" gallery_masonry_grid_ratio="" gallery_masonry_width_double="" hover_type="" lightbox="yes" lightbox_content="" bordersize="" bordercolor="" border_radius="" hide_on_mobile="small-visibility,medium-visibility,large-visibility" class="" id=""]
[fusion_gallery_image image="<em><u>hidden link</u></em>" image_id="247|full" link="" linktarget="_self" /]
[fusion_gallery_image image="<em><u>hidden link</u></em>" image_id="244|full" link="" linktarget="_self" /]
[fusion_gallery_image image="<em><u>hidden link</u></em>" image_id="241|full" link="" linktarget="_self" /]
[/fusion_gallery]

So we now don't need to repeat the ids in the [fusion_gallery] shortcode, we need to repeat the actual images inside that like

<!-- wpv-loop-start -->
	[fusion_gallery]
	<wpv-loop>
  		[fusion_gallery_image image="[types field='image-gallery' output='raw'][/types]" image_id="[get_image_id]|full" /]
	</wpv-loop>
	[/fusion_gallery]
<!-- wpv-loop-end -->

Can you help us get that ID?

#1189908

Hi, to be clear you're talking about a repeating image field, not an image field in a repeating field group (RFG), correct? If so, you cannot use a View to loop over the instances of a repeating image field. Instead, you must use the wpv-for-each shortcode, or your own custom PHP code:
https://toolset.com/documentation/user-guides/views-shortcodes/#vf-153482

Can you clarify whether it's a repeating image field, or a single image field in a repeating field group?

#1189948

Hi,
correct, a repeating image field in a normal fields group.

#1189973

The short answer is there's no built-in way to access the image ID in a wpv-for-each loop. I can show you how to access the image URL, but the corresponding ID will require a separate custom shortcode. Still, it's not straightforward to implement that because the maximum number of nested shortcode attributes is finite. In other words you can't do something like this to pass the URL into the get_image_id shortcode:

[fusion_gallery_image image="[types field='image-gallery' output='raw'][/types]" image_id="[get_image_id url='[types field="image-gallery" output="raw"]']|full" /]

Without a URL attribute or some other shortcode attribute in the get_image_id shortcode, it's not possible to know which URL you need to translate into an ID. With the attribute, it's too many levels of nesting for the system to support. Instead, you would need a custom shortcode that does all the looping internally and produces the correct output. I wonder why the fusion gallery image shortcode requires both an image URL and an image ID? What happens if you only give it a URL?

#1189974

I already tried the Avada code without the ID and it renders the image url as some kind of incomplete base64 🙁

#1189993

That's disappointing. I might be able to find a custom solution for you, but this type of code development is technically outside the scope of support we provide here. It goes into the back of my queue and I work on it when the support load is otherwise empty. For a more immediate solution, you could contact an independent professional in our contractors portal: https://toolset.com/contractors

#1190342

Hi Christian,
Really appreciate your effort!

If you look at my initial post, that might be tweaked to accommodate the new Avada code.
Maybe if I put

[fusion_gallery layout="" picture_size="" columns="" column_spacing="10" gallery_masonry_grid_ratio="" gallery_masonry_width_double="" hover_type="" lightbox="yes" lightbox_content="" bordersize="" bordercolor="" border_radius="" hide_on_mobile="small-visibility,medium-visibility,large-visibility" class="" id=""]

Outside the for each and then loop over $attachment_url and $attachment_id. Then combine the opening fusion_gallery + the loop + closing fusion tag into the shortcode.
Unfortunately I am just not that fluent enough in PHP.

#1201811

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Kristof,

As Christian is currently unavailable. What you can do is to try this custom shortcode that i've built previously. It allows you to get the ID's of all the images in a RFG.

    // Add Shortcode
function get_image_id( $atts ) {
 global $wpdb;

    // Attributes
    $atts = shortcode_atts(
        array(
            'field' => '',
            'id' => ''
        ),
        $atts
    );

    $image_urls = get_post_meta($atts['id'],$atts['field']);
    $id_list = array();
 	foreach ($image_urls as $image_url) {
 		if(!empty($image_url)){
	    		 $attachment = $wpdb->get_col($wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE guid='%s';", $image_url )); 
	    		 array_push($id_list, $attachment[0]);
	    	 }
   	 	}
 	 	
 return implode(",", $id_list); 
 
}
add_shortcode( 'get_image_id', 'get_image_id' );

So how this actually works is that you need to call it like this [get_image_id id='[wpv-post-id]' field='wpcf-field-name']
You can try using this to pass the ids of the image into the fushion gallery shortcode.

Thanks,
Shane

#1203144

Hi Shane, as I hadn't heard back from Toolset, I was lucky to find a solution using another Avada element that only required a single ID which I could loop over, instead of a whole group of ids.
However, I would still like to thank you for your solution, It will surely help other Toolset users.