0

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

1 Answer 1

0

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.

2
  • Thanks. What if I want to construct a theme that retrieves image attachments from the post? Would it be futile without this plugin? Commented Jan 20, 2015 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
    Commented Jan 20, 2015 at 16:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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