[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.
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.
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?
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:
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?
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
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.
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.
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.