Skip Navigation

[Resolved] Outputting a list of IDs for repeating gallery image custom field in custom post

This support ticket is created 3 years, 9 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 2.20 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)

Tagged: 

This topic contains 9 replies, has 2 voices.

Last updated by larryB-3 3 years, 9 months ago.

Assisted by: Shane.

Author
Posts
#1710541

I have a custom post type that has a custom field for a gallery images. I was trying to use some of your previous documenation to output list of image IDs that I can place in a shortcode for another plugin to use.

I tried these https://toolset.com/forums/topic/add-ids-of-repeating-images-to-gallery/ and https://toolset.com/forums/topic/id-like-to-make-a-gallery-from-images-added-in-a-post-type/ without success

custom field is slugged as gallery-image and I was trying to use this shortcode in the post for displaying.

[get_image_id postid="[wpv-post-id]" fieldslug="gallery-image"] - this only returned one ID, and there are 5 plus images in most of the posts

[get_image_ids_repeating post_id="[wpv-post-id]" field="gallery-image"] - nothig is returned

#1710581

Shane
Supporter

Languages: English (English )

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

Hi Larry,

Thank you for getting in touch.

Assuming that your field is a Types field then you will need to do it like this.

[get_image_id postid="[wpv-post-id]" fieldslug="wpcf-gallery-image"]

Adding the wpcf- prefix to the field slug.

Please let me know if this helps.
Thanks,
Shane

#1710583

Shane this only produces the IP for the post I believe and not each of the gallery images. This code only brings back one ID "350",

[get_image_id postid=”350″ fieldslug=”gallery-image”]
[get_image_id postid=”350″ fieldslug=”wpcf-gallery-image”]

Here's a sample page link
hidden link

I want it to bring back all of these IDs.ids="362,361,360,359,358,357,356" for example

#1710589

Shane
Supporter

Languages: English (English )

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

Hi Larry,

Did you add the corresponding code to the Toolset custom code section in Toolset -> Settings -> Custom Code?

If you didn't please add the following code there and ensure you click on the activate button once you've added it.

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

Currently your shortcode isn't rendering on the frontend.

Thanks,
Shane

#1710613
Screen Shot 2020-07-17 at 3.26.20 PM.jpg
Screen Shot 2020-07-17 at 3.26.13 PM.jpg

Yes I have this code in Code Snippets.

I'm still only getting this returned

[get_image_ids_repeating post_id=”350″ field=”gallery-image”]

#1710633

Shane
Supporter

Languages: English (English )

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

Hi Larry,

The shortcode name that you are using is incorrect.

The correct format is.

[get_image_id postid="[wpv-post-id]" fieldslug="wpcf-gallery-image"]

What you have now is.


[get_image_ids_repeating post_id="350″ field="gallery-image"]

Please change it to the correct format and let me know if it starts working.

Thanks,
Shane

#1711141
Screen Shot 2020-07-18 at 8.36.15 AM.jpg

Sorry, Shane. I appreciate the help, but this isn't getting me where I need to be. I want to generate these IDs to convert this:

[gallery columns="6" ess_grid_gal="project-thumnail-slider" ids="362,361,360,359,358,357,356" orderby="rand"] (where these IDs are coming from the images my client uploaded to the gallery field as shown in the screen shot.

to:

[gallery columns="6" ess_grid_gal="project-thumnail-slider" ids="[get_image_id postid="[wpv-post-id]" fieldslug="wpcf-gallery-image"]" orderby="rand"]

#1712711

Shane
Supporter

Languages: English (English )

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

Hi Larry,

Please allow me to have admin access to the website so that I can check on this for you.

I believe the issue here is that the wordpress Gallery shortcode doesn't allow a nested shortcode so you will need to modify the shortcode itself.

Let me know a sample page where I can test this out.

Thanks,
Shane

#1712847

Shane
Supporter

Languages: English (English )

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

Hi Larry,

The shortcode should now be working.

I had to wrap the gallery shortcode inside the custom shortcode as well.

// 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 = attachment_url_to_postid($image_url);
                 array_push($id_list, $attachment);
             }
        }
    $list_of_id = implode(",", $id_list);
  	return do_shortcode("[gallery columns='6' ess_grid_gal='project-thumnail-slider' ids='".$list_of_id."' orderby='rand']");
  
}

add_shortcode( 'get_image_id', 'get_image_id' );

So the final code to run this would be [get_image_id id="[wpv-post-id]" field="wpcf-gallery-image"]

Please let me know if this helps.
Thanks,
Shane

#1712863

My issue is resolved now. Thank you!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.