Need to do a check if post has attachments that are not images. I haven't seen this anywhere - thoughts?

2 Answers 2


You can use the following within a loop:

$files = get_attached_media( $type, $post_id );

Just define the attachment MIME type on $type. Second parameter is optional. Example from the Codex page:

$media = get_attached_media( 'audio', 102 );

With the retrieved array, you can do something like:

if( $media ) {
   //Show the post attachments that are not images, but audio
  • hey thanks. I should specify, I'm looking for documents - .doc, .txt. .pdf, etc.
    – Jason
    Dec 15, 2014 at 16:27
  • Check this: codex.wordpress.org/Uploading_Files, and upload your files. After uploading check the database, and to the posts table. Check the post_mime_type to see the exact MIME type of your file. And it can be any. :) Dec 15, 2014 at 16:31

Here's how to check if the post has an attachment other than image (or other mime types):

        <?php $attachments = get_posts( array(
        'post_type' => 'attachment',
        'posts_per_page' => -1,
        'post_parent' => $post->ID,
        'exclude'     => 'image'

        if ( $attachments ) { ?>
               // do something
            <?php } ?>

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.