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

I have the following gallery order in a shortcode:

[gallery ids="1996,2431,372,447,645,641,647,642,649,643,650,648,653,697,652,390"]

And I have this function, which creates a nicer looking gallery:

add_filter('post_gallery', 'pab_gallery'); 

function pab_gallery($attr) {

    global $post;

        //'order'      => 'ASC',
        'orderby'    => 'menu_order DESC, ID ASC',
        'id'         => $post->ID,
        'columns'    => 3,
        'size'       => 'thumbnail',
        'include'    => '',
        'exclude'    => ''
    ), $attr));

    $attachments = get_children( array('post_parent' => $id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'orderby' => $orderby) );


    $output = '<div class="gallery">';
    $output .= '<ul class="clearfix">';
    foreach ( $attachments as $id => $attachment ) {
        $link = wp_get_attachment_link($id, $size, false, true);
        $output .= sprintf('<li>%s</li>', $link);

    return $output.'</ul></div>';

I have two problems with this: First of all, the order specified by ID is completely ignored, even though I pass "orderby". Additionally, image#1996 is also not displayed by the function, even though it's attached to the post (which I thought isn't necessary anyway when providing IDs to the short codes). This is the order I receive from the plugin :

Array ( [0] => 2431 [1] => 656 [2] => 652 [3] => 651 [4] => 648 [5] => 650 [6] => 643 [7] => 649 [8] => 642 [9] => 647 [10] => 641 [11] => 645 [12] => 629 [13] => 676 )

As you can see it's neither the order I specified using the shortcodes, nor is it in any way ascending or descending.

I want the function to display the IDs in the exact order that they're specified in the shortcodes. Is this possible?

share|improve this question

Since 3.5, the gallery shortcode works a bit differently, the order is embedded within the shortcode itself, in the form of the ids= attribute. In your call to get_posts, change orderby to post__in, and pass the ids as include argument.

For more specifics, look inside wp-includes/media.php at the gallery_shortcode function to see how core does it.

share|improve this answer
How do I retrieve the IDs from the shortcode? – doque May 16 '13 at 9:38

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.