WordPress Get 1 attachment per post from X category outside the loop

Hope someone can help with this. What I want is to display 5 attachments but only 1 attachment from each post from a specific category in the sidebar, which links to the posts permalink.

I’m using the following code so far which gets all attachments from all posts, but some posts have more than 1 attachment and I just want to show the first one, and link it to the permalink of the post.

Read More

So although the limit is 5 posts, if one post has 4 attachments then currently it will show 4 from one, and 1 from the other totalling 5, when what I want it to do is just show 1 from each of 5 different posts.

<?php

$args = array(
    'post_type' => 'attachment',
    'numberposts' => 5,
    'post_status' => null,
    'post_parent' => null, // any parent
    'category_name' => 'work',
    );
$attachments = get_posts($args);
if ($attachments) {
    foreach ($attachments as $post) {
        setup_postdata($post);
        the_title();
        the_permalink();
        the_attachment_link($post->ID, false);
        the_excerpt();
    }
}

?>

Cheers.
Dave

Related posts

Leave a Reply

2 comments

  1. Cheers for that. I haven’t tested it yet but I did manage to get it working using the following code. I’m not a hardcore PHP programmer by any means, so I’m not sure which method is best. All I know right now is that what I have seems to work.

    <?php query_posts('category_name=work&posts_per_page=10'); ?>
                        <?php while (have_posts()) : the_post(); ?>
                            <?php
                            $args = array(
                                'order'          => 'ASC',
                                'post_type'      => 'attachment',
                                'post_parent'    => $post->ID,
                                'post_mime_type' => 'image',
                                'post_status'    => null,
                                'numberposts'    => 1,
                            );
                            $attachments = get_posts($args);
                            if ($attachments) {
                                foreach ($attachments as $attachment) {
                                    //echo apply_filters('post_title', $attachment->post_title);
                                    echo "<li><a href='";
                                    echo the_permalink();
                                    echo "' title='";
                                    echo the_title();
                                    echo "'>";
                                    echo wp_get_attachment_image($attachment->ID, 'thumbnail', false, false);
                                    echo "</a></li>";
                                }
                            }
                            ?>
                        <?php endwhile;?>
    

    Thanks for having a crack at it!

    Dave

  2. Okay, I took a crack at this, I really don’t think there’s any elegant way without using a custom query;

    $attachment_IDs = $wpdb->get_col(
        "SELECT ID FROM $wpdb->posts WHERE
            post_parent > 0 AND
            post_type = 'attachment'
        GROUP BY post_parent
        ORDER BY post_date DESC
        LIMIT 5"
    );
    
    if ($attachment_IDs) {
        $attachments = get_posts(array('include' => implode(',', $attachment_IDs)));
        // do what you like!
    }
    

    The query gets the five most recent attachments whereby each attachment has a unique parent.

    UPDATE:

    Sorry, just realised you wanted to query by category too – add this big old join in after the SELECT;

    INNER JOIN $wpdb->term_relationships ON
        ($wpdb->posts.ID = $wpdb->term_relationships.object_id)
    INNER JOIN $wpdb->term_taxonomy ON
        ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
    

    And add this to the WHERE clause;

    AND $wpdb->term_taxonomy.term_id IN (YOUR_CATEGORY_ID)