0

I am listing all attached images to published posts & pages, however older/removed attached images are being listed. I wonder if this depends on revisions (that shouldn't be considered at all)

Code:

$args = array(
    'post_type' => array('post','page'),
    'post_status' => 'publish',
    'posts_per_page' => '-1'
);


    query_posts( $args );


    while ( have_posts() ) {
        the_post();
        $post = get_post(get_the_ID());
        $attachments = get_children(array('post_parent' => $post->ID,
            'post_status' => 'any',
            'post_type' => 'attachment',
            'post_mime_type' => 'image',
            'order' => 'ASC',
            'numberposts' => '-1',
            'orderby' => 'menu_order ID'));

        foreach ($attachments as $att_id => $attachment) {
            $medium_img_url = wp_get_attachment_image_src($attachment->ID,'medium');
            $image = (isset($medium_img_url[0])) ? $medium_img_url[0] : '';
            ?> <div><a target="_blank" href="<?php echo $image; ?>"><img src="<?php echo $image ?>"/></a></div><?php
         }

    }
    wp_reset_postdata();
  • is there a particular reason you're using get_children rather than a normal WP_Query loop? Also keep in mind that using -1 to pull in unlimited posts can be dangerous, set a limit, even if it's a silly stupid high one you never expect to hit – Tom J Nowell Jan 9 '18 at 15:46
  • Well I thought that it would be natural to loop the posts and get the "children".... As for the limit do you have a suggestion to split the loop somehow? – Riccardo Jan 9 '18 at 18:56
  • Just set the number of posts to a high number you never expect to reach. Showing all posts can lead to dangerous situations, e.g. if you have 10,000 posts to show the page will never load, no matter how many loops, queries or things you put in place. Consider an upper limit of 100 or 200, lower if possible. For anything further, consider using an actual archive with pagination. Also, just use WP_Query, functions like get_pages, get_children are helper methods that wrap around, but they add in additional behaviours that can complicate things. It's just easier to cut out the middle man – Tom J Nowell Jan 9 '18 at 19:30
  • However I wonder why older data is being returned, there might be a bug in the function? – Riccardo Jan 10 '18 at 10:51
  • 1
    Also avoid query_posts like the plague. This probably has something to do with your menu_order and ID – Tom J Nowell Jan 10 '18 at 14:39
1

Your problem is here:

        'order' => 'ASC',
        'numberposts' => '-1',
        'orderby' => 'menu_order ID'));

You asked for them to be ordered by menu order, then ID. Then you asked for them to be given in ascending order.

Since newer posts have higher IDs than older posts, older posts get shown first. Flip the ASC to DESC and you'll likely get what you wanted.

Other notes:

  • Never use query_posts, there is no valid usage of this function that can't be accomplished with WP_Query or the pre_get_posts filter #bestpractice #headache #eldritchhorrors #unforgivablesins
  • Don't bother with all the helper methods for grabbing posts, such as get_pages, get_children, etc, they're all wrappers of WP_Query that add extra complications. You already pass the parent ID anyway #bestpractice
  • Always have an if ( have_posts() ) { check, if no posts were found you'd be told nothing, left wondering what went wrong #debugging
  • Don't use -1, set a super high number, but never set unlimited. Even if you never expect to reach that number, set it anyway. Pagination has performance benefits too, and your server can only hold so much in memory and hold has so much time to generate the page before it reaches the execution time limit #performance #scaling #hightraffic
  • escape your output, don't do echo $url, do echo esc_url( $url ), this guarantees that it is indeed a URL, and that nothing malicious got snook in by a hacker #security

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.