Template that list ALL attachment files
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.
A bit too complex for me, but have you tried using attachment.php as a starting point?
Also, isn’t this
It ONLY bring ONE file, if the post/page/CPT have more than one file inside just show the first.
caused by this
‘posts_per_page’ => -1,
thanx @peter_l but I think removing
posts_per_pagebrackes the query and put a number like 999999 also.
For the attachment.php is like the single.php but for attachment, would be nice to have an archive-attachment.php but it doesn´t exist nether the url wordpress/attachment 🙁
This is untested, but you should only need one query:
$all_files_query = new WP_Query( array( 'post_type' => 'attachment', 'posts_per_page' => -1, 'post_status' => 'inherit', 'post_parent__not_in' => 0 ) );
Sorry, the above code should be:
$all_files_query = new WP_Query( array( 'post_type' => 'attachment', 'posts_per_page' => -1, 'post_status' => 'inherit', 'post_parent__not_in' => array( 0 ) ) );
@Justin_tadlock so I replace the
‘$attachment = get_post`
for the new WP_Query you show me.
But don´t work, I think is becasue first I have a loop selecting just some Post Types and inside that loop I add your WP_Query, could be that ?
- The topic ‘Template that list ALL attachment files’ is closed to new replies.