Support » Plugins and Hacks » Hacks » Filter attachments by menu_order

Filter attachments by menu_order

  • Hello everybody!

    How can I filter the attachments (images) of a post by menu_order? I want to get all the images with menu_order > 0. At the moment I’m using the following code:

    $attachments = get_children( array('post_parent' => $post->ID, 'numberposts' => 0, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order ID') );

    Adding ‘menu_order’ => ‘>0’ to the arguments array didn’t work. I also tried to use the posts_where filter, but it didn’t work either. Any idea how to get this work?

    Thanks for your help!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The simplest way may be to check the menu_order as you loop through the attachments, ie (pseudo-code):

    foreach($attachments as $attachment => $attachment_array ) {
        if( $attachment['menu_order'] > 0 ) {
            // Do your thing here.

    Thank you very much for your reply. I really didn’t consider the way you mentioned. I think I’ll give it a try 🙂

    The more comfortable way would be to get the filtered attachments directly from the database, as I need them to count, set the next / previous link and so on….

    All I want to do is to exclude some images from the gallery without putting id’s into the shortcode. So I want to use 0 or -1 in the order field to ignore them. Maybe I’ll try to extend the built-in gallery with an ‘ignore’ field….


Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Filter attachments by menu_order’ is closed to new replies.