Title: get_post_mime_type
Published: April 25, 2014
Last modified: February 24, 2026

---

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

## In this article

 * [Description](https://developer.wordpress.org/reference/functions/get_post_mime_type/?output_format=md#description)
 * [Parameters](https://developer.wordpress.org/reference/functions/get_post_mime_type/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/functions/get_post_mime_type/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/functions/get_post_mime_type/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/functions/get_post_mime_type/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/get_post_mime_type/?output_format=md#changelog)
 * [User Contributed Notes](https://developer.wordpress.org/reference/functions/get_post_mime_type/?output_format=md#user-contributed-notes)

[ Back to top](https://developer.wordpress.org/reference/functions/get_post_mime_type/?output_format=md#wp--skip-link--target)

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

## 󠀁[Description](https://developer.wordpress.org/reference/functions/get_post_mime_type/?output_format=md#description)󠁿

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

## 󠀁[Parameters](https://developer.wordpress.org/reference/functions/get_post_mime_type/?output_format=md#parameters)󠁿

 `$post`int|[WP_Post](https://developer.wordpress.org/reference/classes/wp_post/)
optional

Post ID or post object. Defaults to global $post.

Default:`null`

## 󠀁[Return](https://developer.wordpress.org/reference/functions/get_post_mime_type/?output_format=md#return)󠁿

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

## 󠀁[Source](https://developer.wordpress.org/reference/functions/get_post_mime_type/?output_format=md#source)󠁿

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

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

    	return false;
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/post.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-includes/post.php#L1230)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-includes/post.php#L1230-L1238)

## 󠀁[Related](https://developer.wordpress.org/reference/functions/get_post_mime_type/?output_format=md#related)󠁿

| Uses | Description | 
| [get_post()](https://developer.wordpress.org/reference/functions/get_post/)`wp-includes/post.php` |

Retrieves post data given a post ID or post object.

  |

| Used by | Description | 
| [WP_REST_Attachments_Controller::handle_featured_media()](https://developer.wordpress.org/reference/classes/wp_rest_attachments_controller/handle_featured_media/)`wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php` |

Determines the featured media based on a request param.

  | 
| [WP_REST_Attachments_Controller::edit_media_item()](https://developer.wordpress.org/reference/classes/wp_rest_attachments_controller/edit_media_item/)`wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php` |

Applies edits to a media item and creates a new attachment record.

  | 
| [wp_image_editor()](https://developer.wordpress.org/reference/functions/wp_image_editor/)`wp-admin/includes/image-edit.php` |

Loads the WP image-editing interface.

  | 
| [wp_generate_attachment_metadata()](https://developer.wordpress.org/reference/functions/wp_generate_attachment_metadata/)`wp-admin/includes/image.php` |

Generates attachment meta data and create image sub-sizes for images.

  | 
| [get_body_class()](https://developer.wordpress.org/reference/functions/get_body_class/)`wp-includes/post-template.php` |

Retrieves an array of the class names for the body element.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/get_post_mime_type/?output_format=md#changelog)󠁿

| Version | Description | 
| [2.0.0](https://developer.wordpress.org/reference/since/2.0.0/) | Introduced. |

## 󠀁[User Contributed Notes](https://developer.wordpress.org/reference/functions/get_post_mime_type/?output_format=md#user-contributed-notes)󠁿

 1.  [Skip to note 2 content](https://developer.wordpress.org/reference/functions/get_post_mime_type/?output_format=md#comment-content-1040)
 2.   [Codex](https://profiles.wordpress.org/codex/)  [  10 years ago  ](https://developer.wordpress.org/reference/functions/get_post_mime_type/#comment-1040)
 3. [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fget_post_mime_type%2F%23comment-1040)
    Vote results for this note: 1[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fget_post_mime_type%2F%23comment-1040)
 4. **Return an icon image path according to the MIME type of the given post**
 5.     ```php
        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;
            default:
              return $base . "file.png";
          }
        }
        // call it like this:
        echo '<img src="'.get_icon_for_attachment($my_attachment->ID).'" />';
        ```
    
 6. WordPress already has a function to get the mime type icon called wp_mime_type_icon
    
    [https://developer.wordpress.org/reference/functions/wp_mime_type_icon/](https://developer.wordpress.org/reference/functions/wp_mime_type_icon/)
 7.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fget_post_mime_type%2F%3Freplytocom%3D1040%23feedback-editor-1040)

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fget_post_mime_type%2F)
before being able to contribute a note or feedback.