Skip Navigation

[Resolved] Create WP gallery from multiple image field

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.

This topic contains 2 replies, has 2 voices.

Last updated by David 4 years, 8 months ago.

Assigned support staff: Nigel.


I am trying to create a WP gallery from a multiple images field called "Property Images", slug "property-images".

I will use the gallery in a content template.

I found these related support threads that look like they will help me, but even so I can't get it to work using either method:

My code in functions.php

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-property-images', false);
    $ids = array();
    foreach ($images as $image) {
        $attachment_id = $wpdb->get_var($wpdb->prepare(
            "SELECT ID FROM $wpdb->posts WHERE guid = %s",
        $ids[] = $attachment_id;
    $out = do_shortcode('[gallery ids="' . implode(",", $ids) . '"]');
    return $out;

My shortcode in template


This returns no output in my single post.

And the other attempt:

function prefix_get_img_ids($atts) {
global $post;
 $images = (array) get_post_meta($post->ID, 'wpcf-property-images', 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('get-image-ids', 'prefix_get_img_ids');

Using shortcode in template

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

This returns only “] in my single post.

Any idea why either of these approaches is not working? Thanks.



Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Hi David

I set this up on a test site to be sure I had it working.

Here is the code I ended up with:

 * Register shortcode gallery_from_image_fields
 * required attributes
 *   postid : the post id
 *   slug : the image field slug (no prefix)
add_shortcode( 'gallery_from_image_fields', function( $atts ){

	global $wpdb;

	$postid = $atts['postid'];
	$slug = 'wpcf-' . $atts['slug'];

	// Retrieve image field urls
	$images = (array) get_post_meta( $postid, $slug, false);

	$ids = array();
	// Convert the urls to post ids for the images
	foreach ($images as $image) {

		$attachment = $wpdb->get_col( $wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE guid='%s';", $image ) ); 
		$ids[] = $attachment[0];

	$ids_list = join( ',', $ids );

	$gallery = '[gallery ids="' . $ids_list . '"]';

	$out = do_shortcode( $gallery );

	return $out;

That registers the shortcode [gallery_from_image_fields] which takes attributes for postid and slug.

You can use it in your Content Template like so:

[gallery_from_image_fields postid='[wpv-post-id]' slug='pictures']

The complication with the above I think arises from nesting other shortcodes inside the WordPress gallery shortcode, which are not parsed correctly.

In any case, the above code worked fine on my test site, hopefully it will for you.


This is an elegant solution. Many thanks.