Black Friday & Cyber Monday sale, save 30%! Valid until November 30th at 11:59:59 PM (UTC - 6). Enter BFCM2015 at checkout or Click here to apply discount →

WordPress 3.6 introduced quite a few new functions that are helpful when working with media attached to a post. One of my personal favorites is a helper function that lets you retrieve the URLs of images that have been inserted as parts of galleries in the post.

The function we will use is get_post_galleries_images(), which will return a multidimensional array of the image URLs belonging to all galleries in the specified post. It returns an array of arrays, one array for each gallery found.

The get_post_gallery_images() return value looks something like this:

    [0] => Array
            [0] => http://localhost/wpms/wp-content/uploads/edd/2013/05/02348_caltonhilledinburgh_1280x1024-190x120.jpg
            [1] => http://localhost/wpms/wp-content/uploads/2013/03/2560x1600_Space-190x120.jpg
            [2] => http://localhost/wpms/wp-content/uploads/2013/03/2649089468_abb2633bc6_o-190x120.jpg
            [3] => http://localhost/wpms/wp-content/uploads/2013/03/02156_orangedays_1024x1024-190x120.jpg
            [4] => http://localhost/wpms/wp-content/uploads/2013/03/02348_caltonhilledinburgh_1280x1024-190x120.jpg


With this data, we can easily do something simple like this:

Our output is:

Screen Shot 2013-08-04 at 12.46.25 PM

The likely hood of you actually wanting to output the plain URLs is pretty slim, but this is just meant as an illustration of how you can use it. I can imagine the function being very helpful in scenarios where a developer needs to know a list of all assets used.

Note, this will return the URLs of all images included in galleries of the post; if you want to only retrieve images for the first gallery, you can use the get_post_gallery_images() function.

    • Pippin

      Sure enough, fixing!

  1. Matt

    Hey buddy. Good post. Right along the lines of what I am working on.

    I am using get_post_gallery which returns a multidimensional array that includes, among other things, the attachment ids of the images in the first gallery found in a post. Using get_post_galleries would return the same info for all galleries in the post.

    Depending on what one wants to do, this is maybe a little more useful because you can get other image sizes as well as things like a link to the full image using wp_get_attachment_url.

    Here’s the essence of what I am doing:

    Now to work on that regular expression… which I have but I’m not sure how to match the first only.

    btw… did you write the codex page?

    • Pippin

      Yours is definitely a more useful and realistic example 😀

      Yes I wrote the codex page.

    • Daphne

      you saved me. i was struggling for hours to fetch all gallery images for a post (and not just the ones attached to the post). thanks mate.

  2. Matt

    I am using this regular expression to remove the short code from the content: and it works great (though I need to figure out how to match the first only for my case).

    • Manda

      Wow nice and clean code Matt, it’s really help me solve my problem

  3. Wade Spane

    Looks like I can finally find some use for the wordpress gallery. I come from the design side of things and do a lot of sites for photographers and other designers. I’ve been using ACF for creating galleries and sliders and such but this looks great.,,

  4. Wordpressor

    Is it possible to change dimensions of these images in links with that function? For example replace these something-190×120.jpg thumbnails with full images? It’d be cool! Thanks!

Your email address will not be published. Required fields are marked *

Error: Please enter a valid email address

Error: Invalid email

Error: Please enter your first name

Error: Please enter your last name

Error: Please enter a username

Error: Please enter a password

Error: Please confirm your password

Error: Password and password confirmation do not match