get_attached_media() returns empty array if media file already used by another post

Question

I wish to retrieve the different PDF and ZIP files attached to a single post and display them in a specific section, outside the content, using the WP function get_attached_media(). While my code works as it should for media files freshly uploaded, I cannot manage to get it working if a file has already been uploaded and used in a different post.

For example, suppose post ID-1 embeds file-1.pdf in the content. Then, if I create post ID-2 and insert again file-1.pdf into the content, get_attached_media( 'application/pdf', $post->ID ) will return an empty array ?!

Am I missing something / should I flush anything (if so, how…) ?

Here is the code of my loop:

loop-single.php

<?php

 if ( have_posts() ) : while (have_posts() ) : the_post(); ?>

    <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>

        <?php

        if ( has_post_thumbnail() ) {
            $thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'item-featured' )['0'];
        } else {
            $thumb = false;
        }

        $content = preg_replace('/[gallery.*]/', '',  wpautop( get_the_content() ) );
        $the_content_EN = get_extended(get_post_meta(get_the_ID(),'mysecondeditor')['0'])['main'];
        $the_content;$date_publication;

        $the_content = apply_filters('the_content', $content );

        // Media files ?
        $pdf = get_attached_media( 'application/pdf', $post->ID );
        $zip = get_attached_media( 'application/zip', $post->ID );
        $attachments = ($pdf || $zip) ? true : false;

        // DEBUG info
        print_r($pdf);
        print_r($zip);

        ?>

        <div class="project">
            <h3 class="entry-title">
                <?php the_title(); ?>
            </h3>

            <div class="left">
                <?php if($thumb): ?>
                    <span class="project-img"><img src="<?php echo $thumb; ?>" alt /></span>
                <?php endif; ?>
                <?php if ($attachments) : ?>
                <h4>Downloads:</h4>
                <ul class="project-files lino">
                    <?php if ($pdf) foreach($pdf as $m) : ?>
                        <li>
                            <a href="<?php echo ( wp_get_attachment_url($m->ID) ); ?>" target="_blank" class="pdf"><i class="fa fa-file-pdf-o"></i>
                                <?php echo ( wp_get_attachment_link($m->ID) ); ?>
                            </a>
                        </li>
                    <?php endforeach; ?>
                    <?php if ($zip) foreach($zip as $n) : ?>
                        <li>
                            <a href="<?php echo ( wp_get_attachment_url($n->ID) ); ?>" target="_blank" class="zip"><i class="fa fa-file-archive-o"></i>
                                <?php echo ( wp_get_attachment_link($n->ID) ); ?>
                            </a>
                        </li>
                    <?php endforeach; ?>
                <?php endif; ?>
                </ul>
            </div>

            <div class="entry-content right">
                <?php
                    echo $the_content;
                    if ($the_content_EN) :
                ?>
                <hr>
                <section class="lang-en">
                    <?php echo $the_content_EN; ?>
                </section>
                <?php endif; ?>
            </div><!-- .entry-content -->
        </div>

        <div class="clear"></div>

    </div><!-- #post-## -->

    <div class="entry-utility">
        <?php edit_post_link( __( 'Edit', 'ywp' ), '<span class="edit-link">', '</span>' ); ?>
    </div><!-- .entry-utility -->

<?php endwhile;endif;// end of the loop. ?>
0
, , Flo 8 months 0 Answers 159 views 0

Leave an answer