WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

The WordPress gallery shortcode now looks like this:

[gallery ids="3439,3440,3444,3443,3445,3453,3457,3458,3456,3461,3452,3468"]

My problem is I have a summary page grabbing the first image of every child page. The code is something like this:

$args        = array(
    'post_type'     => 'attachment',
    'post_parent'   => $page->ID,
    'orderby'       => 'menu_order ID',
    'order'         => 'asc',
    'numberposts'   => 1,
$attachments = get_posts( $args ); 

This now doesn't work (I'm guessing because gallery order is now based on the ID's) Any way to fix it?

share|improve this question
up vote 4 down vote accepted

I had the same issue and was previously using get_children() to retrieve the top 4 images from a gallery for a given page.

Here's the solution I came up with:

// helper function to return first regex match
function get_match( $regex, $content ) {
    preg_match($regex, $content, $matches);
    return $matches[1];

// Extract the shortcode arguments from the $page or $post
$shortcode_args = shortcode_parse_atts(get_match('/\[gallery\s(.*)\]/isU', $post->post_content));

// get the attachments specified in the "ids" shortcode argument
$attachments = get_posts(
        'include' => $shortcode_args["ids"], 
        'post_status' => 'inherit', 
        'post_type' => 'attachment', 
        'post_mime_type' => 'image', 
        'order' => 'menu_order ID', 
        'orderby' => 'post__in', //this forces the order to be based on the order of the "include" param
        'numberposts' => 1,

Now, 'numberposts' => 1 above doesn't seem to work when you specify more than one id in the include parameter. So, you have a couple of choices:

  1. Explode the "ids" argument and grab only the first ID for use in your get_posts call
  2. Grab only the first attachment returned from get_posts (but you'll still be retrieving all attachments in the gallery). Not ideal, but will work.

Hope that helps!

share|improve this answer
thanks! just what I needed :) – numediaweb Mar 8 '13 at 20:10
Glad that worked! If that answers your question, would you mind marking it as "answer" in addition to the plus one? Thanks! – anderly Mar 9 '13 at 22:21
Thanks very much anderly! – chael Mar 11 '13 at 3:14
One quick question, I've managed to make this work (grabbing one image from each post) however it's the last image in the list of ID's instead of the first? – chael Mar 11 '13 at 10:36
So, using the sample above, are you saying that $attachments[0] above is the last image in the list of IDs? – anderly Mar 12 '13 at 3:45

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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