Skip Navigation

[Resolved] How to grab the IDs of an image field with multiple instances?

This support ticket is created 5 years, 2 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.

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)

Tagged: 

This topic contains 21 replies, has 2 voices.

Last updated by peterZ 5 years, 2 months ago.

Assisted by: Shane.

Author
Posts
#1541061

Tell us what you are trying to do?
I want to grab the IDs of an image field with multiple instances to create a masonry gallery with a shortcode from another plugin.

Is there any documentation that you are following?
I tried to follow this: https://toolset.com/documentation/user-guides/custom-content/create-image-galleries-using-toolset-and-custom-fields/
But there is some Info missing how to use this with external galleries

What is the link to your site?
sturmstudios.at

#1541243

Shane
Supporter

Languages: English (English )

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

Hi Peter,

Thank you for getting in touch.

You can actually use the custom shortcode below in order to get this.

    // 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' );

Add it to your Toolset->Settings -> Custom Codes and then activate it.

To use it you will do this [get_image_ids field='field-slug' id='[wpv-post-id]']

Replace the field-slug with the actual slug of the repeatable field.

Please let me know if this helps.
Thanks,
Shane

#1541267
Bildschirmfoto 2020-03-05 um 21.27.04.png

Unfortunately this doesn't work. I put the right slug into the shortcode so it is: [get_image_ids field='taschengaleriebild' id='[wpv-post-id]']
Still doesn't put out the gallery right :/ the code is active.

#1541277

Shane
Supporter

Languages: English (English )

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

Hi Pete,

If you were to place the shortcode outside your masonry gallery shortcode does it work ?

Unfortunately if the shortcode is unable to accept another shortcode as a parameter then there is nothing we can really do.

Please let me know if the shortcode that i've provided returns the ID's of the images.

Thanks,
Shane

#1541281

The shortcode isn't getting executed at all.
I changed every get_image_id to get_image_ids in the custom code to match the shortcodes name - didn't work either.

#1541327

Shane
Supporter

Languages: English (English )

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

Hi Peter,

Would you mind allowing me to have admin access to the site to check on this for you ?

The private fields have been enabled for your next response.

Thanks,
Shane

#1541371

Shane
Supporter

Languages: English (English )

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

Hi Peter,

I've made some adjustments to the code and it should now be returning the ID's of the images.

However it seems that your masonry grid shortcode isn't able to handle the shortcode as a parameter. There isn't anything we can do at this point since the code now provides the ID's for the item.

Thanks,
Shane

#1541373

Hi Shane,
yes the IDs are returned now. But with the triple shortcode it doesn't work. Can I define variables inside a view?

#1542111

Shane
Supporter

Languages: English (English )

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

Hi Peter,

Here is the final shortcode that i've modified to include the current post ID.

// Add Shortcode
function get_image_ids( $atts ) {
 global $wpdb;
    // Attributes
    $atts = shortcode_atts(
        array(
            'field' => '',
            'id' => get_the_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 = attachment_url_to_postid($image_url);
                 array_push($id_list, $attachment);
             }
        }
         
 	return implode(",", $id_list);   
}
add_shortcode( 'get_image_ids', 'get_image_ids' );

It is able to still take the ID as a parameter but if an ID isn't provided it gets the ID from the view loop. As you can see it still doesn't work. This definitely means the parameter isn't able to handle shortcodes.

Thanks,
Shane

#1542125

Hi Shane,

the parameter is able to handle shortcodes. I am doing that in other ones. But having a shortcode inside a shortcode inside a shortcode doesn't work.
That is why I am asking if I can use variables in Toolset to help myself out here.

Thanks!

#1542147

Shane
Supporter

Languages: English (English )

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

Hi Peter,

As it is right now the shortcode doesn't take another shortcode as a parameter.

The custom [get_image_ids] shortcode is able to now function without the ID parameter.

You can use it like this [get_image_ids field='wpcf-my-images']

This is what is now added to the masonry shortcode in this format and it still doesn't work. Is it that it is expecting the shortcode to just be [get_image_ids]

Please let me know.
Thanks,
Shane

#1542149

Shane
Supporter

Languages: English (English )

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

This is what it looks like now.

[dss_masonry_gallery _builder_version="4.3.4" images='[get_image_ids field="wpcf-taschengaleriebild"]' hover_enabled="0"][/dss_masonry_gallery]
#1542235

Hi SHane,

why are you ignoring my question? I am asking if I can use variables indisde the view. Then I can work around this problem very easily.

Thanks,
Peter

#1542249

Shane
Supporter

Languages: English (English )

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

Hi Peter,

I'm not ignoring the question. However when you say variable are you referring to PHP variables? if that is so then no you are not able to do this directly.

The variable would need to be wrapped in a shortcode for you to be able to use it in views.

Though what you can do so that we have 100% clarity is provide an example of how you want your masonry shortcode to get the values. As you say you want to use variables but could you provide an example of what this would look like in the masonry shortcode ?

However what seems to be happening is that the masonry isn't accepting the shortcode.

Another solution I can think of is to return the masonry shortcode inside the custom shortcode that i've created. So we would pass the raw ids that we get from the database directly into the shortcode and return the results of the masonry grid code. All this as a single combined shortcode.

So instead of this .

[dss_masonry_gallery _builder_version="4.3.4" images='[get_image_ids field="wpcf-taschengaleriebild"]' hover_enabled="0"][/dss_masonry_gallery]

You will just have a custom shortcode.

[cust_masonry _builder_version="4.3.4" images='[get_image_ids field="wpcf-taschengaleriebild"]' hover_enabled="0"]

Then we will rebuild the dss_masonry_gallery shortcode inside the cust_masonry shortcode when we render it using the do_shortcode() function.

Please let me know what you think of this as it should be able to work for your case.

Thanks,
Shane

#1542263

Hi Shane,
yes, I meant PHP-variables. Unfortunate, that I can't use them.

Yes, this would actually be the solution I would implement now.
I'll do that, thank you.