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();
get_children
rather than a normalWP_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 hitWP_Query
, functions likeget_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 manquery_posts
like the plague. This probably has something to do with yourmenu_order
andID