1

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?

<?php

    $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' );
    }}
?>
2

2 Answers 2

1

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

0

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' );
        }

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.