How do I get post attachments in menu_order in WordPress?

I’m struggling to get menu_order to work for images attached to a post. I am trying to pull all of the images attached to a specific post in the order specified via the drag-and-drop interface in the media window. Here is the code pulling the images:

$args = array(
    'post_type' => 'attachment',
    'post_mime_type' => 'image',
    'output' => 'ARRAY_N',
    'order' => 'ASC',
    'orderby' => 'menu_order',
    'post_parent' => $post_id /* This is defined elsewhere */
);

$images =& get_children($args);
This is not pulling the images in the order shown in when looking at the attached images in the media window of the post edit page. The order is instead ordered by date. Can anyone shed some light on this issue? I disabled the category order plugin I was using, and this did not remedy the problem.

Read More

EDIT:

Thanks to gdaniel for your help! I realized after following your advice and checking the image posts in my database that they all had a menu_order value of 0. I was able to temporarily fix this issue using the code in this forum post.

I’m looking for a way now to change the menu order on the event of drag and dropping images. Thanks to kaiser for his suggestions!

Related posts