How do I exclude all images from a wp_query?

I want to query attachments and exclude all images.

I can see how to include only images, using 'post_mime_type' => 'image/*', but I couldn’t find any way of achieving the opposite. Is there any mime_type equivalent of posts__not_in?

3 Answers
3

Pretty much the solution is to include all mimes except images. WordPress has a nifty little function where it keeps all it’s accepted mime-types called get_allowed_mime_types() ( cleverly named ) which returns an Array() of mimes. All we need to do is get the difference between the returned array and the array of mime-types we don’t want in our query:

$unsupported_mimes  = array( 'image/jpeg', 'image/gif', 'image/png', 'image/bmp', 'image/tiff', 'image/x-icon' );
$all_mimes          = get_allowed_mime_types();
$accepted_mimes     = array_diff( $all_mimes, $unsupported_mimes );
$attachment_query   = new WP_Query( array(
    'post_type'         => 'attachment',
    'post_status'       => 'inherit',
    'post_mime_type'    => $accepted_mimes,
    'posts_per_page'    => 20,
) );

Leave a Comment