How would I go about listing only unattached media in say a sidebar?

I have a client who uses PDF files for their newsletter, I don’t think that this is a good idea personally, but it’s not negotiable. They have a news page where they put real time news and they’d like to add a sidebar to this page where there would be links to download these PDF files which would update automatically when they add them to the media library. So that the page doesn’t over populate we’ve agreed that only the 12 most recent newsletters should be shown, that’s 1 for each month of the year.

They have no plans to add media of any other type without it being attached directly to posts or pages, so I figure that just a list of the 12 most recent unattached media files should suffice. Is there a way that this can be done using say a custom loop or WP_query object? I really have no idea how to go about starting this.

Read More

I’ve done all sorts of workarounds in all sorts of ways regarding custom loops and sidebars so that’s not the issue, what I really need to know is if there is an option for the loop to only display unattached media, or some WordPress function that lists media.

I can’t really give any examples or snippets of my code because I haven’t started anything yet.

EDIT: Okay, figured it out.

Okay, I found a pretty straight forward sort of solution. It’s not perfect because it’s taking excessive amounts of steps per newsletter but it works exactly how I want it to.

This is what I’ve got sitting in my sidebar:

        <?php query_posts('category_name=newsletter&number_of_posts=12');
              if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

              // From here down I found on a site which I'll cite below.
              <?php $args = array(
                    'post_type' => 'attachment',
                    'numberposts' => null,
                    'post_status' => null,
                    'post_parent' => $post->ID
              );
              $attachments = get_posts($args);
              if ($attachments) {
                    foreach ($attachments as $attachment) {
                          echo '<li>';
                          the_attachment_link($attachment->ID, false);
                          echo '</li>';
                    }
              } ?>

              <?php endwhile; endif; ?>
              <?php wp_reset_query; ?>

I made a category called Newsletter and the only thing I put in each post is a title and attach a PDF, the title doesn’t even carry through, but it’s necissary to publish the post.

Finally, here is the link to the source for that code. I love these guys.

Related posts

Leave a Reply

3 comments

  1. You could also use ‘post_parent’ => 0. All unattached attachments are assigned ‘post_parent’ = 0. Here is where I found that. I’m sure that there is a more authoritative resource somewhere, but this worked for me. 🙂

  2. Okay, figured it out.

    Okay, I found a pretty straight forward sort of solution. It’s not perfect because it’s taking excessive amounts of steps per newsletter but it works exactly how I want it to.

    This is what I’ve got sitting in my sidebar:

        <?php query_posts('category_name=newsletter&number_of_posts=12');
              if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    
              // From here down I found on a site which I'll cite below.
              <?php $args = array(
                    'post_type' => 'attachment',
                    'numberposts' => null,
                    'post_status' => null,
                    'post_parent' => $post->ID
              );
              $attachments = get_posts($args);
              if ($attachments) {
                    foreach ($attachments as $attachment) {
                          echo '<li>';
                          the_attachment_link($attachment->ID, false);
                          echo '</li>';
                    }
              } ?>
    
              <?php endwhile; endif; ?>
              <?php wp_reset_query; ?>
    

    I made a category called Newsletter and the only thing I put in each post is a title and attach a PDF, the title doesn’t even carry through, but it’s necissary to publish the post.

    Finally, here is the link to the source for that code. I love these guys.

  3. Queries for something that doesn’t exist are … tricky and probably inefficient. I would suggest another route:

    1. Add a taxonomy to the uploaded PDF files. You could use a plugin like Media Tags for that.
    2. Use get_posts() to get the last 12 attachments from this taxonomy.
    3. Done.