<?php
// See: https://facetwp.com/how-to-filter-wp-attachments-and-draft-pending-or-private-posts/#display-only-certain-types-of-attachments
// The following will work if the query ONLY retrieves the 'attachment' post type:
$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 );
$args = array(
'post_type' => [ 'attachment' ],
'post_status' => [ 'publish', 'inherit' ], // 'inherit' is needed for attachments
'posts_per_page' => 15,
'post_mime_type' => $accepted_mimes // only allow attachments that are not images
);
// The above will NOT work if there are also OTHER post types than attachments in the query, like posts or pages.
// In this case you need to use 'post_where' as shown below. See: https://wordpress.stackexchange.com/a/209720
add_filter('posts_where', function($where, $query) {
global $wpdb;
// Check if 'post_type' exists in the query and is an array
if ( isset($query->query['post_type'] ) && is_array( $query->query['post_type'] )) {
// Check if the query includes "attachment" in the post type argument
if ( in_array( 'attachment', $query->query['post_type'] ) ) {
$where .= ' AND ' . $wpdb->posts . '.post_mime_type NOT LIKE \'image/%\'';
}
}
return $where;
}, 10, 2);