WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am using the following simple get attachment code to display all images attached to a certain post but I want to be able to exclude the images uploaded via a couple of custom meta boxes that are also attached to the post.

For example, how could I exclude an image uploaded with a meta key of sample_image_1 from this code?


    $args = array (
        'post_type' => 'attachment',
        'numberposts' => -1,
        'post_status' => null,
        'post_parent' => $post->ID,
        $attachments = get_posts($args);
        if ($attachments) {
        foreach ( $attachments as $attachment ) {
        the_attachment_link( $attachment->ID , 'screenshot' );
share|improve this question
do you use plugin to make meta box? – amit Aug 1 '12 at 14:06
No, for this project I am using this script - github.com/jaredatch/Custom-Metaboxes-and-Fields-for-WordPress – Dean Elliott Aug 1 '12 at 14:07
up vote 1 down vote accepted

Get all of the attachment IDs from your meta fields, put them in an array, and pass that as exclude parameter of get_posts.

share|improve this answer

We can store the meta_key value in some variable suppose $metacheck and output the attachent only if the variable is not present.

        $metacheck = get_post_meta($attachment->ID, 'sample_image_1', false);
        //output only if metacheck has null value
        if (!$metacheck) {
        the_attachment_link( $attachment->ID , 'screenshot' );
share|improve this answer

Your Answer


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.