I think the problem is that you’re passing the wrong arguments to the $attachments
query, causing you not to get the intended posts in the $attachments
query.
Here’s what you’re doing:
$args = array(
'post_status' => 'inherit',
'numberposts' => 0,
'post__not_in' => array_merge($do_not_duplicate,get_option( 'sticky_posts' )),
'post_type' => 'attachment',
);
$args['tax_query'] = array(
array(
'taxonomy' => 't-arte',
'terms' => $term_id,
'field' => 'id',
),
);
$attachments = get_posts( $args );
So, you’re querying all posts that are post-type attachment
, rather than only the posts that are attached to the current post being looped through in $media_query
.
Here’s how you loop through $media_query
:
foreach ($media_query as $media_query) :
(Note: bad form. Try something like foreach ( $media_query as $media ) :
instead.)
You need to pass the ID of the current post to your $attachments
query, as post_parent
. Something simple might be:
$attachments = get_posts( array(
'post_status' => 'inherit',
'post_type' => 'attachment',
'post_parent' => $media_query->ID
'numberposts' => 0
) );