How do I exclude all images from a wp_query?

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