0

I have a situation where I need to get all the images used in some kind of post. Now the problem is that attachments is saved only when I upload new image, if I use some other image which was already uploaded, it doesn't change my post attachment.

So even if I change all the images in post, Wordpress still see that my post attachment is that very first uploaded image, not the new one. Any ideas how should I rewrite the attachments even if image is not a new one?

  • Do you mean only featured images or also images that are inserted in the body of a post? – cjbj Sep 19 '16 at 15:25
  • Yes, I'm talking about Featured images and images uploaded via ACF plugin. Well if I select to upload a new image - it works, post gets an attachments. But if I select some image which was already uploaded and used in another post - attachment doesn't change. – The50 Sep 20 '16 at 7:09
  • 1
    You are not answering my question. You say "I need to get all the images used in some kind of post". Would that be only the featured images or also the images that are inserted in the body of a post. The latter is a much more difficult problem. – cjbj Sep 20 '16 at 9:26
  • No, I don't need the images used inside main content. – The50 Sep 20 '16 at 13:42
1

To get all featured images belonging to a certain post type, you'd have to get all those posts and the loop through them to get the featured image. Like this:

$query = new WP_Query( array( 
  'post_type'     => 'yourposttype',
  'posts_per_page'=>-1,
  ));

$thumb_list = array ();
if ( $query->have_posts() ) {
  while ( $query->have_posts() ) {
    $query->the_post(); 
    $thumb_list[] = get_post_thumbnail_id();
    }
  wp_reset_postdata(); // resetting for the main query
  }

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.