WordPress: how to set Featured image in custom post type


When you create a new custom type in WordPress, you need to use Featured image, how to set Featured image in custom post type?

Step 1: you must place thumbnail in supports array
See example below (for post type: actor):

add thumnail to custom post type

function ts_add_support_type() {
	$rewrite_slug = 'actor';
	$args = array(
		'label' => __('Actors','localdomain'),
		'description' => '',
		'public' => true,
		'has_archive'	=>true,
		'show_ui' => true,
		'show_in_menu' => true,
		'capability_type' => 'post',
		'map_meta_cap' => true,
		'hierarchical' => false,
		'menu_icon'	=>	'dashicons-video-alt',
		'rewrite' => array('slug' => $rewrite_slug, 'with_front' => true),
		'query_var' => 'actor',
		'supports' => array('title','editor','publicize','comments','thumbnail','author','post-formats','custom-fields'),
		'labels' => array (
			  'name' => 'Actor',
			  'singular_name' => __('Actor','localdomain'),
			  'menu_name' => __('Actors','localdomain'),
			  'add_new' => __('Add Actor','localdomain'),
			  'add_new_item' => __('Add New Actor','localdomain'),
			  'edit' => __('Edit','localdomain'),
			  'edit_item' => __('Edit Actor','localdomain'),
			  'new_item' => __('New Actor','localdomain'),
			  'view' => __('View Actor','localdomain'),
			  'view_item' => __('View Actor','localdomain'),
			  'search_items' => __('Search Actors','localdomain'),
			  'not_found' => __('No Actor Found','localdomain'),
			  'not_found_in_trash' => __('No Actor Found in Trash','localdomain'),
			  'parent' => __('Parent Actor','localdomain'),
			)
	);
	 
	register_post_type('actor', $args); 
}
add_action( 'init', 'ts_add_support_type' , 1 );

Step 2: You must add theme support for this post type

See example below (for post type: actor):

add_theme_support('post-thumbnails', array('post','actor'));

add thumnail to custom post type success

Done! Now, you can use Feature image for this post type.

Leave a Reply