Metabox Not Showing on Custom Post Type But On Pages and Post

by Behseini   Last Updated January 13, 2018 18:08 PM

I have a Custom Post Type like

function cpt_Projects() {
    $labels = array(
        'name'                 => 'Projects',
        'singular_name'        => 'Project',
        'menu_name'            => 'Projects',
        'name_admin_bar'       => 'Projects',
        'parent_item_colon'    => 'Parent Projects:',
        'all_items'            => 'All Projects',
        'view_item'            => 'View Project',
        'add_new_item'         => 'Add New Project',
        'add_new'              => 'Add New Project',
        'new_item'             => 'New Projects',
        'edit_item'            => 'Edit Project Item',
        'update_item'          => 'Update Project Item',
        'search_items'         => 'Search Project Item',
        'not_found'            => 'Project Not found',
        'not_found_in_trash'   => 'Project Not found in Trash',
    );
    $args = array(
        'label'                => 'ProjectsCPT',
        'description'          => 'This Post Type Adds Eyeglasses to Website',
        'labels'               => $labels,
        'supports'             => array( 'title', 'thumbnail', 'editor'),
        'taxonomies'           => array( 'ProjectsTax' ),
        'register_meta_box_cb' => 'add_details_metabox',
        'hierarchical'         => true,
        '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',
    );
    register_post_type( 'ProjectsCPT', $args );

}

add_action( 'init', 'cpt_Projects', 0 );

and a Metabox like

  function add_details_metabox($post_type) {
    $types = array('post', 'page', 'ProjectsCPT');

   if (in_array($post_type, $types)) {
      add_meta_box(
        'details-metabox',
        'Project Details',
        'detail_meta_callback',
        $post_type,
        'normal',
        'high'
      );
   }
  }

after running the code the Metabox is showing on all Pages and Posts but not on Custom Post Type ProjectsCPT Can you please let me know what I am doing wrong? (It works fine if I remove the if statement

 if (in_array($post_type, $types)) {}

but this add metabox to all Posts and Pages which is not what I need to do



Answers 3


Post type names cannot contain capital letters. So behind the scenes, your CPT is probably called projectscpt rather than ProjectsCPT - hence it not matching the value in your array.

vancoder
vancoder
May 23, 2016 18:58 PM

NOTE the difference between add_meta_boxes and register_meta_box_cb.

When you register the meta boxes using add_meta_boxes, here is how WordPress calls do_action()

/**
 * Fires after all built-in meta boxes have been added.
 *
 * @since 3.0.0
 *
 * @param string  $post_type Post type.
 * @param WP_Post $post      Post object.
 */
do_action( 'add_meta_boxes', $post_type, $post );

The first argument is post type and the second one is post object.

And when callback is registered using register_meta_box_cb then in register_post_type() function WordPress add an action.

if ( $args->register_meta_box_cb )
        add_action( 'add_meta_boxes_' . $post_type, $args->register_meta_box_cb, 10, 1 );

Then

/**
 * Fires after all built-in meta boxes have been added, contextually for the given post type.
 *
 * The dynamic portion of the hook, `$post_type`, refers to the post type of the post.
 *
 * @since 3.0.0
 *
 * @param WP_Post $post Post object.
 */
do_action( 'add_meta_boxes_' . $post_type, $post );

So the first and only argument is $post the post object and you are using it as post type.

You have to get the post type before comparing.

Example:-

function add_details_metabox($post) {
    $types = array('post', 'page', 'projectscpt');
    $post_type = get_post_type($post);

    if (in_array($post_type, $types)) {
        add_meta_box(
            'details-metabox',
            'Project Details',
            'detail_meta_callback',
            $post_type,
            'normal',
            'high'
        );
    }
}

NOTE #1 As @vancoder answered, no matter what case you pass in post type. It is converted to lowercase. What WordPress recommends to register post type without spaces and capital letters. You must use projectscpt instead of capital letters.

NOTE #2 When using register_meta_box_cb it hook the callback function on add_meta_boxes_{post_type} action. This meta box will not appear on any other post type. Therefore, you do not need to check the post type in callback function. If you are getting meta box on other post types too then check the code, perhaps you made some mistake.

Sumit
Sumit
May 23, 2016 19:14 PM

Make sure you add your custom post type in the add_meta_box function arguments for $screen.

add_meta_box( string $id, string $title, callable $callback, string|array|WP_Screen $screen = null, string $context = 'advanced', string $priority = 'default', array $callback_args = null )

Example: add_meta_box( 'my_id', My Title, 'my_callback_function', 'my_post_type', 'normal', 'default' );

ISRAIL AHMED
ISRAIL AHMED
January 13, 2018 17:27 PM

Related Questions





Start Post Edit Page With All MetaBoxes Closed

Updated July 03, 2018 15:08 PM

Find callback function of custom meta box

Updated February 19, 2018 10:08 AM