0

I am new to WordPress. I have created a template page where I want to show single posts, So to get media of that post I am using a function

$args = array(
  'post_type' => 'attachment',
  'post_status' => 'inherit',
  'post_mime_type' => 'image',
  'posts_per_page' => -1,
  'post_parent__not_in' => array(0)
);
$attachment = new WP_Query($args);
$attachment = $attachment->posts;

but by this function I am getting all the media images and when I edit the post and remove these images and add new other images.

This function always give me every image, like it always gives all the images old removed and currently added. I need only current images not removed.

Is there any function that give me the current images of the post not removed?

0

Hmm... Try altering your query a little to:

$args = array(
    'post_parent' => $post_id, 
    'post_status' => 'inherit', 
    'post_type' => 'attachment', 
    'post_mime_type' => 'image',
    'order' => 'ASC', 
    'orderby' => 'menu_order ID',
);
$attachment = new WP_Query( $args );
$attachment = $attachment->posts;

Alternatively, you could use the following:

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

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.