WordPress set featured image with custom post type

In wp-admin, I create a new admin menu page.

add_menu_page('My custom post page type title', 'My custom post type menu', '', 'my-custom-slug', '', '', 99);
add_submenu_page('my-custom-slug', 'Add new', 'Add new', 'manage_options', 'post-new.php?post_type=my-custom-post-type', '');

I want to use the panel like post-new.php and edit.php so I registered a custom post type.

Read More
register_post_type('my-custom-post-type', 
                   array('labels'=>array('name'=>__('Products','text-domain'),
                                         'singular_name'=>__('Product','text-domain'),
                                         'menu_name'=>_x('Products','Admin menu name','text-domain'),
                                         'add_new'=>__('Add Product','text-domain'),
                                         'add_new_item'=>__('Add New Product','text-domain'),
                                         'edit_item'=>__('Edit Product','text-domain'),
                                         'new_item'=>__('New Product','text-domain'),
                                         'view_item'=>__('View Product','text-domain'),
                                         'not_found'=>__('No Products found','text-domain'),
                                         'not_found_in_trash'=>__('No Products found in trash','text-domain')),
                         'supports'=>array('title','editor','thumbnail','comments')
                         'rewrite'=>array('slug'=>'mscases'),
                         'public'=>true,
                         'capability_type'=>'post'));

The custom menu page works fine, and the meta box Featured Image works fine too, I can pick image in media library.

After I choose an image, it doesn’t appear on Featured Image meta box, and the admin-ajax.php response is -1 ( I check the post page, It is zero if success ).

But if I change the parameter my-custom-post-type to product ( like woocommerce ), the featured image I pick up will appear.

Is there any thing I miss in coding?

Related posts

4 comments

  1. Replace and try

    register_post_type('my-custom-post-type',
        array('labels' => array('name' => __('Products', 'text-domain'),
                'singular_name' => __('Product', 'text-domain'),
                'menu_name' => _x('Products', 'Admin menu name', 'text-domain'),
                'add_new' => __('Add Product', 'text-domain'),
                'add_new_item' => __('Add New Product', 'text-domain'),
                'edit_item' => __('Edit Product', 'text-domain'),
                'new_item' => __('New Product', 'text-domain'),
                'view_item' => __('View Product', 'text-domain'),
                'not_found' => __('No Products found', 'text-domain'),
                'not_found_in_trash' => __('No Products found in trash', 'text-domain')),
            'supports' => array('title', 'editor', 'thumbnail', 'comments'),
            'rewrite' => array('slug' => 'mscases'),
            'public' => true,
            'capability_type' => 'post'));
    
  2. When you creating the custom post type never use the ‘-‘ sign please use ‘_’ instead of ‘-‘.

    I have same problem in past and i have changed and it working for me.

  3. Googling about this issue, finally I found out the problem is.

    You have to use the add_action and set the hook as init.

    Such as add_action('init', array('MyClass', 'RegisterPostType'));

    And the featured image works fine.

  4. You have to tell WordPress first that your theme supports featured image by placing this at functions.php :

    add_action( 'after_setup_theme', 'umbrella_theme_setup' );
    function umbrella_theme_setup(){
        add_theme_support('post-thumbnails');
    }
    

    And than you need to enable featured image on custom posts :

    register_post_type('my-custom-post-type', 
                       array('labels'=>array('name'=>__('Products','text-domain'),
                                             'singular_name'=>__('Product','text-domain'),
                                             'menu_name'=>_x('Products','Admin menu name','text-domain'),
                                             'add_new'=>__('Add Product','text-domain'),
                                             'add_new_item'=>__('Add New Product','text-domain'),
                                             'edit_item'=>__('Edit Product','text-domain'),
                                             'new_item'=>__('New Product','text-domain'),
                                             'view_item'=>__('View Product','text-domain'),
                                             'not_found'=>__('No Products found','text-domain'),
                                             'not_found_in_trash'=>__('No Products found in trash','text-domain')),
                             'supports'=>array('title','editor','thumbnail','comments')
                             'rewrite'=>array('slug'=>'mscases'),
                             'public'=>true,
                             'capability_type'=>'post'));
    

    Featured image meta should appear now on your custom posts and the_post_thumnail(); functions should work.’
    And you have to remove the sub_menu_page and menu_page as WordPress shows a UI for them so you don’t have to prepare one.

Comments are closed.