Retrieving post attachments (3 posts)

  1. starapple
    Posted 4 years ago #

    Greetings. I'm trying to retrieve attachment urls to the post being added/edited in the visual text editor, using a pop-up window similar to the Add Media window, from within my theme. However, the code I've hacked together lists all files of the attachment type in the Media Library rather than just those for the post. How to I get only files for the post being worked with? This is the code that lists mp4s:

    function mp4s(){
        global $post;
        $args = array( 'post_type' => 'attachment', 'numberposts' => -1, 'post_mime_type' => 'video/mp4', 'post_status' => null, 'post_parent' => $post->ID );
        $attachments = get_posts($args);
        if ($attachments) {
                foreach ( $attachments as $attachment ) {
                        $src = wp_get_attachment_url( $attachment->ID);
                        if ($src) {echo '<option>'.$src.'</option>';}

    This shows only attachments for the post if I use it in a meta box in the admin, but not in the pop-up accessed via an icon I've added to the visual editor through the theme's functions.php.


  2. starapple
    Posted 4 years ago #


  3. starapple
    Posted 4 years ago #

    Thanks. I found a round-about way or retrieving the post ID, but a way nonetheless.

Topic Closed

This topic has been closed to new replies.

About this Topic