Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

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.

share|improve this question
    
It's great that you found the solution yourself. To mark this question as solved, can you please add your solution as a answer below and mark it as accepted? If you edit it in your question it will remain in the "unanswered" list. –  Jan Fabry May 27 '11 at 19:30
add comment

3 Answers

up vote 3 down vote accepted

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. :)

share|improve this answer
    
Changing this to the correct answer. It's simpler and requires less steps. I'd like to not that it's an alteration to my answer simply so that when others are reading this it doesn't get confused. –  DouglasMarken Jun 11 '13 at 9:46
add comment

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.

share|improve this answer
add comment

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.
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.