1

I need to get all file attachments which is not an image,
'post_mime_type' only accepts "any" or specific mime types,

How to grab all type of attachments, but images?

1 Answer 1

4

WordPress has a function, get_allowed_mime_types, which will return all allowed types. We can filter this list and exclude any types containing image, then query for all remaining types by passing them as a comma separated list to post_mime_type. This may not be the most efficient way to do it, you may be better off filtering posts_where, but it'll work.

$filtered_mime_types = array();

foreach( get_allowed_mime_types() as $key => $type ):
    if( false === strpos( $type, 'image' ) )
        $filtered_mime_types[] = $type;
endforeach;

$args = array(
    'post_type' => 'attachment',
    'posts_per_page' => -1,
    'post_status' => 'any',
    'post_mime_type' => implode( ',', $filtered_mime_types )
);

$results = new WP_Query( $args );
2
  • It seems get_allowed_mime_types will be solve my problem, but that was very boring problem on wp
    – Digerkam
    Commented Feb 14, 2013 at 13:28
  • post_mime_type also takes array as an value, no need to implode()
    – Sisir
    Commented Oct 12, 2017 at 15:20

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.