Title: Multiple Custom Post Types
Last modified: August 31, 2016

---

# Multiple Custom Post Types

 *  Resolved [ajwah](https://wordpress.org/support/users/ajwah/)
 * (@ajwah)
 * [10 years, 1 month ago](https://wordpress.org/support/topic/multiple-custom-post-types-3/)
 * I have about three different custom post types, each of which should have different
   type of fields for which I would like to use CMB2 accordingly.
    The three custom
   post types with their respective cmb2 fields are supposed to be:
 * > section => title
   >  subsection => image upload subsubsection => textarea
 * However, when trying to add cmb2 fields to their respective custom post types,
   all three cmb2 fields are only added to the first one and the other two are bereft
   of any field, e.g:
 * > section => title, image upload, textarea
   >  subsection => NOTHING subsubsection
   > => NOTHING
 * It seems to be that cmb2 overrides the regular `Post` object that all three custom
   post types are inheriting from.
 * I have taken note of the following already
    [https://wordpress.org/support/topic/cmb2-and-custom-post-type?replies=17](https://wordpress.org/support/topic/cmb2-and-custom-post-type?replies=17)
 * Either cmb2 is only meant to be used for regular posts and pages and thus I am
   using it for a purpose it has not been made, either I am using it incorrectly,
   either I am not registering my custom post type in a correct way.
 * This is the way I am registering a custom post type:
 *     ```
       add_action( 'init', 'create_subsubsection_type' );
       function create_subsubsection_type() {
       	$labels = array(
             'name'                => _x( 'Subsubsection', 'Post Type General Name', 'my_theme' ),
             'singular_name'       => _x( 'Subsubsection', 'Post Type Singular Name', 'my_theme' ),
             'menu_name'           => __( 'Subsubsection', 'my_theme' ),
             'parent_item_colon'   => __( 'Parent Subsubsection', 'my_theme' ),
             'all_items'           => __( 'All Subsubsection', 'my_theme' ),
             'view_item'           => __( 'View Subsubsection', 'my_theme' ),
             'add_new_item'        => __( 'Add New Subsubsection', 'my_theme' ),
             'add_new'             => __( 'Add New', 'my_theme' ),
             'edit_item'           => __( 'Edit Subsubsection', 'my_theme' ),
             'update_item'         => __( 'Update Subsubsection', 'my_theme' ),
             'search_items'        => __( 'Search Subsubsection', 'my_theme' ),
             'not_found'           => __( 'Not Found', 'my_theme' ),
             'not_found_in_trash'  => __( 'Not found in Trash', 'my_theme'
            ),
         );
   
         register_post_type( 'subsubsection',
           array(
             'labels' => $labels,
             'supports' => array(''), // Remove all existing fields
             'public' => true,
             'has_archive' => true,
           )
         );
       }
       ```
   
 * All the others are registered exactly the same, except for a different name.
 * This is how I use cmb2, as mentioned in the documentation:
 *     ```
       function register_subsub_metabox() {
   
       	$cmb_subsub = new_cmb2_box( array (
       		'id' => 'metabox',
       		'title' => __( 'Subsubsection Metabox', 'cmb2' ),
       		'object_types' => array( 'subsubsection' ),
       	) );
   
       	$cmb_subsub->add_field( array(
       	    'id'      => 'subsub_title',
       	    'name'    => 'Title',
       	    'desc'    => 'Provide a title.',
       	    'default' => '',
       	    'type'    => 'text'
       	) );
   
       	$cmb_subsub->add_field( array(
           'id'      => 'subsub_icon',
           'name'    => 'Icon',
           'desc'    => 'Upload an icon or enter a URL.',
           'type'    => 'file',
           'options' => array(
               'url' => false, // Hide the text input for the url
           ),
           'text'    => array(
               'add_upload_file_text' => 'Add Icon'
           ),
       	) );
   
       	$cmb_subsub->add_field( array(
       	    'id'      => 'subsub_description',
       	    'name'    => 'Description',
       	    'desc'    => 'Enter a brief description',
       	    'default' => '',
       	    'type'    => 'textarea'
       	) );
       };
       add_action( 'cmb2_admin_init', 'register_subsub_metabox' );
       ```
   
 * [https://wordpress.org/plugins/cmb2/](https://wordpress.org/plugins/cmb2/)

Viewing 6 replies - 1 through 6 (of 6 total)

 *  Plugin Author [Justin Sternberg](https://wordpress.org/support/users/jtsternberg/)
 * (@jtsternberg)
 * [10 years, 1 month ago](https://wordpress.org/support/topic/multiple-custom-post-types-3/#post-7369740)
 * I copied your CPT and CMB2 registration snippets exactly, and works as expected:
   [http://b.ustin.co/aWmh](http://b.ustin.co/aWmh).
 * But your original statement was that there are 3 CPTs and it’s not working with
   all of them. If that is the case, I suggest you post your entire CPT and CMB2
   registration code. (Probably best to do [in a gist](https://gist.github.com/)).
 *  Thread Starter [ajwah](https://wordpress.org/support/users/ajwah/)
 * (@ajwah)
 * [10 years, 1 month ago](https://wordpress.org/support/topic/multiple-custom-post-types-3/#post-7369785)
 * It is working for only one CPT.
    All the fields that were supposed to be registered
   over 3 CPTS are included in one CPT. Will make gist soon.
 * I am happy that it is supposed to work out of the box with support for different
   CPT’s.
    The only thing I can imagine to have messed up is the index.php loading.
   I first included the plugin from admin panel, but as it was not working, I copied(
   not cut and paste) the cmb2 directory to my local theme and used the bootstrap
   method accordingly.
 * Will post gist soon
 *  Thread Starter [ajwah](https://wordpress.org/support/users/ajwah/)
 * (@ajwah)
 * [10 years, 1 month ago](https://wordpress.org/support/topic/multiple-custom-post-types-3/#post-7369797)
 * Here is the gist with all the relevant cpts and cmb2
    [https://gist.github.com/Ajwah/7aed15abf589b2a04db79f3e88ec5fd5](https://gist.github.com/Ajwah/7aed15abf589b2a04db79f3e88ec5fd5)
 *  Plugin Author [Justin Sternberg](https://wordpress.org/support/users/jtsternberg/)
 * (@jtsternberg)
 * [10 years, 1 month ago](https://wordpress.org/support/topic/multiple-custom-post-types-3/#post-7369805)
 * It’s because you are using the same box ID for 3 different registrations:
 * [https://gist.github.com/Ajwah/7aed15abf589b2a04db79f3e88ec5fd5#file-subsection-php-L35](https://gist.github.com/Ajwah/7aed15abf589b2a04db79f3e88ec5fd5#file-subsection-php-L35)
   
   [https://gist.github.com/Ajwah/7aed15abf589b2a04db79f3e88ec5fd5#file-subsubsection-php-L35](https://gist.github.com/Ajwah/7aed15abf589b2a04db79f3e88ec5fd5#file-subsubsection-php-L35)
   [https://gist.github.com/Ajwah/7aed15abf589b2a04db79f3e88ec5fd5#file-topics-php-L35](https://gist.github.com/Ajwah/7aed15abf589b2a04db79f3e88ec5fd5#file-topics-php-L35)
 * Make those unique per CPT, or use one cmb2 metabox for all 3 CPTs. (`'object_types'
   => array( 'subsections', 'subsubsection', 'topics' ),`) and you should be fine.
 *  Thread Starter [ajwah](https://wordpress.org/support/users/ajwah/)
 * (@ajwah)
 * [10 years, 1 month ago](https://wordpress.org/support/topic/multiple-custom-post-types-3/#post-7369808)
 * I made the changes and it now works properly, thanks!
 * For future reference in case someone has the same question:
 * This:
 *     ```
       $cmb_subsection = new_cmb2_box( array (
       		'id' => 'metabox',            // The same id was used for all other cpt
       		'title' => __( 'Subsection Metabox', 'cmb2' ),
       		'object_types' => array( 'subsections' ),
       	) );
       ```
   
 * should change to:
 *     ```
       $cmb_subsection = new_cmb2_box( array (
       		'id' => 'subsection_metabox', //Unique id
       		'title' => __( 'Subsection Metabox', 'cmb2' ),
       		'object_types' => array( 'subsections' ),
       	) );
       ```
   
 *  Plugin Contributor [Michael Beckwith](https://wordpress.org/support/users/tw2113/)
 * (@tw2113)
 * The BenchPresser
 * [10 years, 1 month ago](https://wordpress.org/support/topic/multiple-custom-post-types-3/#post-7369809)
 * Awesome to see resolution here 🙂

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘Multiple Custom Post Types’ is closed to new replies.

 * ![](https://ps.w.org/cmb2/assets/icon.svg?rev=2866672)
 * [CMB2](https://wordpress.org/plugins/cmb2/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/cmb2/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/cmb2/)
 * [Active Topics](https://wordpress.org/support/plugin/cmb2/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/cmb2/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/cmb2/reviews/)

## Tags

 * [cpt](https://wordpress.org/support/topic-tag/cpt/)

 * 6 replies
 * 3 participants
 * Last reply from: [Michael Beckwith](https://wordpress.org/support/users/tw2113/)
 * Last activity: [10 years, 1 month ago](https://wordpress.org/support/topic/multiple-custom-post-types-3/#post-7369809)
 * Status: resolved