WordPress.org

Ready to get started?Download WordPress

Forums

Template that list ALL attachment files (8 posts)

  1. lonchbox
    Member
    Posted 11 months ago #

    Hi,

    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:

    1. It ONLY bring ONE file, if the post/page/CPT have more than one file inside just show the first.
    2. 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.

    Pancho

  2. Peter_L
    Member
    Posted 11 months ago #

    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,

  3. lonchbox
    Member
    Posted 11 months ago #

    thanx @Peter_l but I think removing posts_per_page brackes 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 :(

  4. Justin Tadlock
    Member
    Posted 11 months ago #

    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
    	)
    );
  5. Justin Tadlock
    Member
    Posted 11 months ago #

    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 )
    	)
    );
  6. lonchbox
    Member
    Posted 11 months ago #

    weird, post_parent__not_in works without the array :P

  7. lonchbox
    Member
    Posted 11 months ago #

    @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 ?

  8. lonchbox
    Member
    Posted 11 months ago #

    Well I think is not correct how I´m doing this. THe new question is, How can I new if the attachment is inside a post and how can I get that posts ID?

    Thanx guys for the help :)

Reply

You must log in to post.

About this Topic