I have some images uploaded in wordpress posts (eg. post#1, and post#2). Now I've created a new post (eg. Post#3), and in this post I have inserted the WordPress 3.5 gallery, I did not upload any image in the post#3, instead I used the images from the post#1 and post#2. Now in the post#3, I would like to get the links to the images, but I can not seem to get. I am using the following code:

$attachments = get_children( array('post_parent' => get_the_ID(), 'post_type' => 'attachment', 'post_mime_type' =>'image') );
    foreach ( $attachments as $attachment_id => $attachment ) {
            echo wp_get_attachment_image( $attachment_id, 'medium' );

It will show only the image if I upload in the post, but it won't show the image if I add from the other posts. So, how can I get the links to the images which are added in the gallery but not uploaded.

Thanks for any help.

4 Answers 4


I had the same problem - how can I display all the images from a post containing a gallery where some of the images are not attached to the post, or are attached to different posts?

The new media interface in WP3.5 allows you to add any of the images in the media library to a gallery, regardless of whether they are "attached" or not. As you've discovered, the "get_children" function only returns images that are attached to the post. The trick I used is to get the ids from the [gallery] shortcode itself rather than from the fact that they are attachments. The shortcode includes all of the image ids, regardless of whether they are attached to the post or not. E.g. [gallery ids="410,411,412,413,414,415"]. Obviously, wordpress can parse this to retrieve all the images and display them in a gallery format using the "gallery_shortcode" function included in the core of wordpress (in includes/media.php). To get the ids of all images in the [gallery] short code I made a new function in my functions.php file:

function grab_ids_from_gallery() {

global $post;
$attachment_ids = array();
$pattern = get_shortcode_regex();
$ids = array();

if (preg_match_all( '/'. $pattern .'/s', $post->post_content, $matches ) ) {   //finds the     "gallery" shortcode and puts the image ids in an associative array at $matches[3]
$count=count($matches[3]);      //in case there is more than one gallery in the post.
for ($i = 0; $i < $count; $i++){
    $atts = shortcode_parse_atts( $matches[3][$i] );
    if ( isset( $atts['ids'] ) ){
    $attachment_ids = explode( ',', $atts['ids'] );
    $ids = array_merge($ids, $attachment_ids);
  return $ids;

add_action( 'wp', 'grab_ids_from_gallery' );

Now just use the "grab_ids_from_gallery()" function to return all the ids as an array in your template.

I'm sure there is a more elegant way to do this - But this works for me. The basis for this code came from:


which discusses this issue.


I must admit I haven't had the time to play with 3.5's new media galleries and the like, but working off of the code alone without regard to WP's Gallery functionality...

The code you have supplied uses get_children() with the query variable post_parent set to get_the_ID() (the ID of the post object currently being processed in the content loop). As such, the call to get_children() is only capable of querying attachments to the current post, and no others.

In the terminology of your example, your code, when executed in the context of the loop while processing Post#3, cannot retrieve the attachments attached to Post#1 and Post#2 because it is specifically asking for the attachments that are attached to Post#3.

Unfortunately, Wordpress does not support passing multiple values into the post_parent query var, so in order to retrieve the attachments from the other posts in the manner you desire, you would have to obtain the post IDs of the attachment-containing posts (Post#1 and Post#2) and execute a call to get_children() for each, substituting the post_parent value of "get_the_ID()" with the respective ID.

A more efficient (though theoretically more complex) solution may be possible making clever use of the WP_Query object, however I believe that the solution you are seeking (or that I assume you are seeking - you never actually asked a question ;) ) would look something along the lines of

$attachmentParentIds = getAttachmentParentIds( get_the_ID() );
foreach( $attachmentParentIds as $attParentId ) {
    $attachments = get_children( array('post_parent' => $attParentId, 'post_type' => 'attachment', 'post_mime_type' =>'image') );
    foreach ( $attachments as $attachment_id => $attachment ) {
        echo wp_get_attachment_image( $attachment_id, 'medium' );

which uses a function like

//Returns an array of post IDs containing attachments that should be displayed
//on the post indicated by the passed post ID.
function getAttachmentParentIds( $intPostId ) {
    $ids = array();

    //Figure out which posts contain attachments that need to be displayed for $intPostID


    return $ids; //return an array of IDs

as a placeholder for the logic that determines which posts contain attachments that need to be displayed for the passed post ID. I am not positive as to the extent of your implementation nor your application, and as such I know not what the specifics of that logic might entail regarding your project.


The real solution would be the creation of a new "real" object "gallery" with its own ID.

A gallery is no more than an array of image ids, but it would be easier to manage:

  • You can create it directly from the Media Manager, in the way you create the new "galleries" now in WP 3.5.x Instead of generating a shortcode like this: [gallery ids="xx, yy, zz"] it could simply create a new "Gallery" with its own id and call it like this: [gallery galley_id="123"]

  • You could manage your "real galleries" objects under media, in a new "Galleries" section

  • You'd have a set of functions to manage such an object. In that way the OP's problem would be solved just by passing the gallery_id to use in the attachment.php template (or a new gallery.php template that could be implemented as well) using the corresponding function.

  • You'd be able to mix up pictures attached to various post in a gallery object without worrying about the original post.

  • You could add multiple galleries to a post in a cinch.

If you think about it, this is NextGen galleries in a nutshell. No wonder people use that plugin a lot. Unfortunately I don't think that such an important feature should be deferred to a plugin, especially a pretty bloated and heavy one like Nextgen, which packs a lot of superfluous features.

In this sense the new media manager's logic is a bit flawed, i think. It kinda suggest that you are "creating" a new entity called gallery while it's actually just merging together a bunch of images ids for you.

I understand that you won't see the flaw if you are using the Carousel display from Jetpack, since it won't take you out of the current page and you won't need to get the image tied to that specific gallery you "created".

Nevertheless I think there's a dangerous gap between the UI of the new media manager and what it actually does to show a gallery.

$galleries              =  $wpdb->get_results("SELECT meta_value  FROM $wpdb->postmeta WHERE (meta_key = '_product_image_gallery' AND post_id = '". $post_id ."')");
    $gal_images_ids         = $galleries[0]->meta_value;
    foreach (explode(",", $gal_images_ids) as $gal_img_id) {
        $gal_images_url     = wp_get_attachment_url($gal_img_id);
        $gal_image_title    = end(explode("/", $gal_images_url));
  • Is this is specifically a WooCoomerce product image gallery? You probably want to get the real title of the image too rather than just taking the filename part.
    – Rup
    Oct 5, 2020 at 8:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.