get_post_mime_type( int|WP_Post $post = null ): string|false

Retrieves the mime type of an attachment based on the ID.


This function can be used with any post type, but it makes more sense with attachments.

Top ↑


$post int|WP_Post Optional
Post ID or post object. Defaults to global $post.

Default: null

Top ↑


string|false The mime type on success, false on failure.

Top ↑


File: wp-includes/post.php. View all references

function get_post_mime_type( $post = null ) {
	$post = get_post( $post );

	if ( is_object( $post ) ) {
		return $post->post_mime_type;

	return false;

Top ↑


Version Description
2.0.0 Introduced.

Top ↑

User Contributed Notes

  1. Skip to note 1 content
    Contributed by Codex

    Return an icon image path according to the MIME type of the given post

    function get_icon_for_attachment($post_id) {
      $base = get_template_directory_uri() . "/images/icons/";
      $type = get_post_mime_type($post_id);
      switch ($type) {
        case 'image/jpeg':
        case 'image/png':
        case 'image/gif':
          return $base . "image.png"; break;
        case 'video/mpeg':
        case 'video/mp4': 
        case 'video/quicktime':
          return $base . "video.png"; break;
        case 'text/csv':
        case 'text/plain': 
        case 'text/xml':
          return $base . "text.png"; break;
          return $base . "file.png";
    // call it like this:
    echo '<img src="'.get_icon_for_attachment($my_attachment->ID).'" />';

    WordPress already has a function to get the mime type icon called wp_mime_type_icon

You must log in before being able to contribute a note or feedback.