Featured Image in Custom Post Type – Explained with Code

When you create a new custom post type or you already have one, sometimes you notice the featured image option or meta box does not exist in your custom post type.

In this article, we will not only take a look into adding a featured image option to a custom post type but will also understand the way to get the custom post type featured image in the frontend.

Why Featured Image Not Showing in Custom Post Type

If you are not seeing the option to add featured image in your custom post type, that means your custom post type doesn’t support the featured image or post thumbnail.

In that case, you have to enable the featured image option for your custom post type.

Enable Featured Image Option in Custom Post Type

To add the featured image option for your custom post type, you have to add the thumbnail support with it. There are two ways to that:

Method 1:

Just add the code given below to your theme’s functions.php file and add your custom post type name in it.

add_post_type_support( 'your-custom-post-type-name', 'thumbnail' );

This is an quick easy way to enable the featured image support to custom post type.

However if you want to enable and keep it with your custom post type code, then you can follow the second method.

Method 2:

  1. Find your custom post type code (where you have register this custom post type)
  2. Find 'supports' parameter inside the array of your custom post type arguments.
  3. Add 'thumbnail' argument to the 'supports' array of your custom post type arguments. As shown and highlighted in the code given below.
 $args = array(
        'label'               => __( 'Events', 'twentytwenty' ),
        'description'         => __( 'Event news and reviews', 'twentytwenty' ),
        'labels'              => $labels,
          'supports'            => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments'),
        'hierarchical'        => false,
        'public'              => true,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_nav_menus'   => true,
        'show_in_admin_bar'   => true,
        'menu_position'       => 5,
        'can_export'          => true,
        'has_archive'         => true,
        'exclude_from_search' => false,
        'publicly_queryable'  => true,
        'capability_type'     => 'post',
        'show_in_rest' => true,
    );

After enabling the featured image support, you can add the image to your custom post type. Now let’s understand how we will get this image and display to the frontend of the website.

Get Featured Image of Custom Post Type

You can get the custom post type featured image by using the_post_thumbnail() function inside the your custom post type loop or templates file.

<?php the_post_thumbnail(); ?>

However, If you want to show featured image outside the loop or any other location instead of custom post template file, then you can use get_the_post_thumbnail($post_ID) function. With this function, you have to pass the custom post ID.

<?php echo get_the_post_thumbnail($post_ID); ?>

You can also get custom post type featured image URL by using get_the_post_thumbnail_url($post_ID) function and use it with the image tag to display the featured image.

<img src="<?php echo get_the_post_thumbnail_url($post_ID);?>" />

Remove Featured Image Option in Custom Post Type

To remove custom post type featured image option, add the code given below to your theme’s functions.php file, and replace the custom post type name in it.

This will disable the featured image option for the given custom post type.

function diwp_remove_post_type_support() {

    remove_post_type_support( 'your-post-type-name', 'thumbnail' );
}
add_action( 'init', 'diwp_remove_post_type_support');