Skip Navigation

[Resolved] need help to get ID’s of gallery images for standard WP gallery

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

Problem: I would like to create a standard WordPress gallery using the gallery shortcode, and I would like to be able to provide a comma-separated list of image IDs. My images are stored as custom fields in a repeatable field group (RFG).

Solution: Use Views to query the RFG and output a list with separators. Use a custom shortcode to output the image ID in the loop.

Here's a shortcode that will return an image ID given a post ID and a field slug:
[php]
function wpt_get_img_id($atts) {
  global $post, $wpdb;
  $atts = shortcode_atts([
    'postid' => 0,
    'fieldslug' => '',
  ], $atts);
  $postid = isset($atts['postid']) ? $atts['postid'] : $post->ID;
 
  $img = get_post_meta($postid, 'wpcf-' . $atts['fieldslug'], true);
 
  $query = "SELECT ID FROM {$wpdb->posts} WHERE guid='$img'";
  $id = $wpdb->get_var($query);
 
  return $id;
}
add_shortcode("get_image_id", "wpt_get_img_id");

You would use it like this in a View:

[get_image_id postid="[wpv-post-id]" fieldslug="my-image-field"]

Relevant Documentation:
https://toolset.com/documentation/user-guides/view-layouts-101/#list-with-separators

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.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 6 replies, has 2 voices.

Last updated by poulP 5 years, 10 months ago.

Assisted by: Christian Cox.

Author
Posts
#1205592
support-toolset.png

I am trying to get the ids from uploaded images (gallery/repeatable field). I use the guide here:
https://toolset.com/forums/topic/get-id-of-repeater-image-field-insted-of-url/
And I have put the

function prefix_get_img_ids

from the guide into the functions.php of my theme.
Next I have put the new registered shortcode

[get-image-ids]

into a content-template, and used the content-template in a view for the Repeatable field groups Gallery.
Finally the view is used in content-template for post type "person/member".

Link to a page where the issue can be seen: hidden link

With the shortcode [get-image-ids] I expected to get a comma-separated list of the image ID's. The guide uses

return implode(",",$ids);

....but I only get the ID's with white space between.
It was my plan to put the shortcode into

[gallery ids="[get-image-ids]"]

, but it dont work if there is no comma between ID's.

It seems for me that the glue string in the implode dont work...your example dont out put the "comma"(,) in the output.

I have following code in my functions.php file (same as your guide, just few changes to aplly to my site):

function hent_galleri_ids($atts) {
global $post;
  
 $images = (array) get_post_meta($post->ID, 'wpcf-galleri-billede', false); // cast to array in case there is only one item
 $ids = array();
    
 global $wpdb;
     
 foreach($images as $img) {
  $query = "SELECT ID FROM {$wpdb->posts} WHERE guid='$img'";
  $id = $wpdb->get_var($query);
  $ids[] = $id;
 }  
 return implode(",",$ids);
}
add_shortcode("galleri-ids", "hent_galleri_ids");

In the content-template for the view I only have:

[galleri-ids] 

In the uploaded image you can see the 5 images (retreived by another view), and on the right the 5 ID's...but not as a comma-separated list.
Thanks,
Poul

#1205700

and used the content-template in a view for the Repeatable field groups Gallery.
The problem is the example you're referencing was not meant to be used with repeatable field groups (RFG). It was meant for use with a repeating image field. It looks like there are 5 results in your View of RFG, and each result includes one gallery image. That's why there are no commas.
Result 1: 1187
Result 2: 1184
Result 3: 1182
Result 4: 1161
Result 5: 1105

Are you trying to include a repeating image field in each RFG, or just one image in each RFG?

#1206269

Hi Christian,
yes now I have used the example with repeating image field instead of, and it works as as expected (output comma-separated image ID's).

But I would still ask you if it is possible to having another similar function/shortcode in functions.php, so that it works on single images in each RFG, meaning that the function could output the a comma-separated list of the single images uploaded in each RFG.

The target is to having an gallery, where my users can upload images with image title and image description.

The RFG consists of an image upload field, a image title field and a image description field

The RFG and post form/view where I include the RFG works perfect for, but as mentioned I would like to put the uploaded images (with titles) into a jetpack tiled gallery though the [gallery ids=""]. And for this I need the comma-separated list.

Can you adjust the example to work on this?

Thanks

#1206419

But I would still ask you if it is possible to having another similar function/shortcode in functions.php, so that it works on single images in each RFG, meaning that the function could output the a comma-separated list of the single images uploaded in each RFG.
This code is more complicated, because you must:
1. Query the RFG using the post relationships API
2. Loop over the RFG results and get the image field URL for each result
3. Query the database again for each image field URL to retrieve the image ID
4. Output the array of image IDs as a comma-separated list

Or, you could create a View of the RFG using the Loop Wizard option "List with separators" and use a custom shortcode to output the image ID inside the loop. https://toolset.com/documentation/user-guides/view-layouts-101/?utm_source=viewsplugin&utm_campaign=views&utm_medium=edit-view-wizard&utm_term=Learn%20about%20different%20layouts#list-with-separators

Let me know which you would prefer and we can work on a solution.

#1206762

Hi Christian,
I think the solution with view of the RFG with comma-separators is the best.
But could you provide an example of the custom shortcode to use inside the loop?
I can alter your example with the values from my setup.
Thanks

#1207141

Here's a shortcode that will return an image ID given a post ID and a field slug:

function wpt_get_img_id($atts) {
  global $post, $wpdb;
  $atts = shortcode_atts([
    'postid' => 0,
    'fieldslug' => '',
  ], $atts);
  $postid = isset($atts['postid']) ? $atts['postid'] : $post->ID;

  $img = get_post_meta($postid, 'wpcf-' . $atts['fieldslug'], true);

  $query = "SELECT ID FROM {$wpdb->posts} WHERE guid='$img'";
  $id = $wpdb->get_var($query);

  return $id;
}
add_shortcode("get_image_id", "wpt_get_img_id");

You would use it like this in a View:

[get_image_id postid="[wpv-post-id]" fieldslug="my-image-field"]
#1207398

My issue is resolved now. Thank you!