I´m trying to create a Template where list all files attached in a post, page some custom post types. It´s like a a "Download" page for all files inside those post_types. So I get it in a way but not completly.
First I use a WP_query to bring the post_types I want:
<?php $all_files = array( 'post_type' => array('papers','post','page'), 'posts_per_page' => -1, ); $all_files_query = new WP_Query($all_files);?> <?php if ($all_files_query->have_posts()) : ?> <?php while ($all_files_query->have_posts()) : $all_files_query->the_post(); ?>
Then inside the loop I add a get_post to bring the attachments and filtered by some post_mime_type:
<?php $attachments = get_posts( array( 'post_type' => 'attachment', 'post_mime_type' => 'application/pdf,application/msword,application/vnd.ms-powerpoint,application/vnd.ms-excel,application/zip,audio,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.openxmlformats-officedocument.presentationml.presentation,application/vnd.openxmlformats-officedocument.wordprocessingml.document,application/vnd.openxmlformats-officedocument.presentationml.presentation', 'posts_per_page' => -1, 'post_parent' => $post->ID, 'exclude' => get_post_thumbnail_id() ) );?> <?php if ( $attachments ): ?> <?php foreach ( $attachments as $attachment ): ?> <?php $class = "post-attachment mime-" . sanitize_title( $attachment->post_mime_type ); $thumbimg = wp_get_attachment_url( $attachment->ID ); ?> <a class="source-file-link" href="<?php echo $thumbimg; ?>">
This works in some way, list all post who have an attachment file inside it´s editor. But I get this problems:
- It ONLY bring ONE file, if the post/page/CPT have more than one file inside just show the first.
- If a file is attach at the same time in two different post just shows the newest post.
Here I´m stock, can´t find a way to fix it.
Is there another way to do this?
Or is a retriction of how wp_query or get_post works?
Thanx for any advice.