Creating a metabox to upload multiple images, Ignoring The Featured Image

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

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.

Leave a Comment