Automatically generates a Wordpress Gallery from all Images associated with the current post https://wp-types.com/forums/topic/create-wp-gallery-from-multiple-image-field/
/**
* 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;
});