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 would really like my images to display in the same way I've ordered them in the media browser. What am I doing wrong?

$images =& get_children( array('post_parent' => $post->ID,
                                               'post_type' => 'attachment',
                                               'post_mime_type' => 'image',
                                               'orderby' => 'menu_order',
                                               'order' => 'asc' ));
share|improve this question
up vote 1 down vote accepted

a) If you have reordered the images under the uploaded to this post in the media browser then they should have menu_order>0 and you can get that order with

'orderby' => 'menu_order',                                               
'order' => 'asc' 

b) If you haven't, then menu_order=0 for all these images and you will need to use

'orderby' => 'ID',                                               
'order' => 'asc' 

to get the same order (or date asc).

A solution for both cases could be like this, in pure SQL:

... ORDER BY menu_order ASC, ID ASC

If you try

'orderby' => 'menu_order ID',                                               
'order' => 'asc' 

you will get

... ORDER BY menu_order, ID ASC

Since get_children() is using get_posts() there are some restriction on how extra fields can be added to the orderby and order parameters .

You can also use your custom query, where you have full control over the double ordering:

$sql="SELECT {$wpdb->posts}.* 
FROM {$wpdb->posts} 
WHERE {$wpdb->posts}.post_parent = %d  
AND ({$wpdb->posts}.post_mime_type LIKE 'image/%%')  
AND {$wpdb->posts}.post_type = 'attachment' 
AND ({$wpdb->posts}.post_status <> 'trash' AND {$wpdb->posts}.post_status <> 'auto-draft')  
ORDER BY {$wpdb->posts}.menu_order ASC,{$wpdb->posts}.ID ASC";

share|improve this answer
awesome complete answer. thanks! – rob-gordon Mar 2 '13 at 23:12

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.