4

My question is basically identical to this other question here, however my question is still slightly different. I basically have a custom post type called "Packages" each package can have a slideshow with its own images.

I understand the images are uploaded through the media uploader (as per the associated questions chosen answer), however is it possible to only get a list of images attached to the post that AREN'T the featured image?

My understanding is that the answer given to the other question would get all images including the featured image, does Wordpress treat featured images differently in the background so I can exclude them?

2 Answers 2

6

Generally speaking, I would take the approach of querying for post attachments, but withhold the attachment that is the post thumbnail. WP provides a simple way of finding the post thumbnail ID with get_post_thumbnail_id (Codex Ref). To modify the code from the other post, I would add the following parameter to the $args array to withhold the thumbnail attachment from being queried:

'post__not_in' => array(
    get_post_thumbnail_id($post->ID)
)

The post__not_in parameter will "Specify post NOT to retrieve." (Codex Ref)

To put the whole thing together, the code would look like:

$args = array(
    'post_type' => 'attachment',
    'numberposts' => null,
    'post_status' => null,
    'post_parent' => $post->ID,
    'post__not_in' => array(
        get_post_thumbnail_id($post->ID)
    )
);
$attachments = get_posts($args);
if ($attachments) {
    foreach ($attachments as $attachment) {
        echo apply_filters('the_title', $attachment->post_title);
        the_attachment_link($attachment->ID, false);
    }
}

In order to further fine tune your queries, I would highly recommend exploring the WP_Query class (Codex Ref). It's power is only rivaled by its ease of use.

1
  • Ah, nice. This solution fits in more with what I need, thanks brotato. This will work beautifully. Commented May 5, 2011 at 5:16
2

You could basically use the code from that other question and then filter out the featured image by looking at the post thumbnail ID.

foreach ($attachments as $attachment)
{
    // Skip the post thumbnail
    if ($attachment->id == get_post_thumbnail_id())
        continue;
}

Codex: get_post_thumbnail_id()

3
  • Amazing that this question has been sitting here for the last few hours and we simultaneously answer it. At first, I was thinking of going the route of checking for the attachment_id when processing the results of the query, but I like the idea of just not getting the row that will not be used. Which do you think would actually be more efficient?
    – tollmanz
    Commented May 5, 2011 at 4:56
  • Thanks for answering Geert. Your solution is pretty good, but I opted for just filtering it out on a query level. Thanks for taking the time to answer though, I appreciate it. Gave you an upvote. Commented May 5, 2011 at 5:17
  • Sardine's approach is the better one. It gives you a clean array without elements you don't need. For example, this is useful if you want to count() the array elements.
    – Geert
    Commented May 5, 2011 at 9:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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