I have been struggling to retrieve image attachments to specific post without success until I found that images inserted into the post via media library are not treated as attachments. That is, you should first upload images to the media library (even if they exists there already) and then insert into the post. Then they are treated as attachments and may be retrieved with this code:

    ?php
$args = array( 'post_type' => 'attachment', 'posts_per_page' => -1, 'post_status' =>'any', 'post_parent' => $post->ID ); 
$attachments = get_posts( $args );
if ( $attachments ) {
    foreach ( $attachments as $attachment ) {
        echo apply_filters( 'the_title' , $attachment->post_title );
        the_attachment_link( $attachment->ID , false );
    }
}
?>

I believe that this is the problem with attachments. However, my question is how can I retrieve images inserted into the post via the media library not uploading images each time?

Thanks

I've struggled with the same thing pretty much always. There's a plugin called File Un-Attach, and a Unattach and Re-attach Media Attachments. See if they work for you.

There's also 5 year old Trac ticket on this, but it seems to be under conversation still.

  • Thanks. What if I want to construct a theme that retrieves image attachments from the post? Would it be futile without this plugin? – user3695711 Jan 20 '15 at 16:10
  • Frankly I'm not sure how that would pan out... The attached images are marked to be children of the post, so you'd have to manage to do that somehow. Look the source in the plugins, maybe that helps. There's also another ticket with some patches core.trac.wordpress.org/ticket/6820 – any_h Jan 20 '15 at 16:16

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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