Does anyone know how to get ID's of images from wordpress gallery in post? I have search entire internet and there is nothing to this subject.

2 Answers 2


You can find out the IDs of which images are included in a gallery by switching to the Text editor tab and look for a piece of code like this:

[gallery columns="3" link="file" ids="615,619,618,617,616"]

The comma-separated list of image IDs are enclosed in the ids attribute

If you're looking to find out IDs of existing images in order to manually create a gallery, you can visit the Media > Library admin menu and click on the Edit link under an image. In your browser's address bar, you should see something similar to the following:


The number after post= is the image ID. In this case, the image ID is 622.

  • I don't want to search ids manually... I need some loop for it.
    – Itachi
    Jan 8, 2013 at 15:54
  • So you want to retrieve the IDs of images attached to a gallery using PHP code?
    – shea
    Jan 8, 2013 at 21:46
  • I want to get images "src" so yes I want theyr id
    – Itachi
    Jan 8, 2013 at 22:07

Assuming that bungeshea is right and you want to retrieve the IDs of images attached to a gallery using PHP, you can filter 'shortcode_atts_gallery', like this:

add_filter ('shortcode_atts_gallery', 'wsec_filter_gallery_atts', 10, 3);

function wpsec_filter_gallery_atts ($out, $pairs, $atts) {
    $ids = $out['include'];
    // Do what you want with the ids
    return $out;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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