WordPress.org

Ready to get started?Download WordPress

Forums

Filter attachments by menu_order (3 posts)

  1. t.leitner
    Member
    Posted 2 years ago #

    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!
    Thomas

  2. Jackson
    Member
    Posted 2 years ago #

    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.
        }
    }
  3. t.leitner
    Member
    Posted 2 years ago #

    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....

    Thanks,
    Thomas

Topic Closed

This topic has been closed to new replies.

About this Topic