Exclude an array

I’m moving edit post dashboard to front end.
So here is a case, I have to queries, one for product attachments and other for ALL.

Now, I want to exclude product attachments from ALL attachments query

Read More

here is an example how to exclude post thumbnail from WordPress Codex:

<?php

$args = array(
    'post_type'   => 'attachment',
    'numberposts' => -1,
    'post_status' => null,
    'post_parent' => $post->ID,
    'exclude'     => get_post_thumbnail_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 );
    }
}

?>

before that, I have a query of all products, so can exclude take array argument? For example with my code

$mediaArgs = array(
    'post_type'         => 'attachment',
    'post_mime_type'    => 'image',
    'numberposts'       => -1,
    'post_status'       => null,
    'post_parent'       => $current_post,
);
$MediaAttachments = get_posts($mediaArgs);

Can I then foreach this and save MediaAttachment->ID inside some array and exclude put it next tu exclude? In my theory it should work as usual, but no matter how I tried, it does not work.

Related posts

Leave a Reply

1 comment

  1. Strange but it works with same principle I was trying to. Maybe I had some grammer error. Here is a code if somebody will need it in future as there was nothing in search result:

    // get product media library
    $mediaArgs = array(
        'post_type'         => 'attachment',
        'post_mime_type'    => 'image',
        'numberposts'       => -1,
        'post_status'       => null,
        'post_parent'       => $current_post, // any parent
    );
    $MediaAttachments = get_posts($mediaArgs);
    
    $parentMediaArray = array();
    foreach ($MediaAttachments as $MediaAttachment) {
        $parentMediaArray[] = $MediaAttachment->ID;
    }
    // get ALL the media library exept that I'm using now
    $mediaArgs2 = array(
        'post_type'         => 'attachment',
        'post_mime_type'    => 'image',
        'numberposts'       => -1,
        'post_status'       => null,
        'exclude'           => $parentMediaArray,
        'post_parent'       => 'any', // any parent
    );
    $MediaAttachmentsALL = get_posts($mediaArgs2);
    

    Where $parentMediaArray is the array which I exclude