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

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