4

I have this function that adds to custom post types which is very simple and straight forward as seen below;

function create_hr_post_type() {

  register_post_type( 'informationtechnology',
    array(
      'labels' => array(
        'name' => __( 'Information Technology' ),
        'singular_name' => __( 'Information Technology' )
      ),
      'public' => true,
      'has_archive' => true,
      'rewrite' => array('slug' => 'informationtechnology'),
    )
  );

  register_post_type( 'humanresource',
    array(
      'labels' => array(
        'name' => __( 'HR' ),
        'singular_name' => __( 'HR' )
      ),
      'public' => true,
      'has_archive' => true,
      'rewrite' => array('slug' => 'humanresource'),
    )
  );


}

add_action( 'init', 'create_hr_post_type' );

However, only one post type is being registered on checking my admin left bar. I only see the the HR post type! What is going on in here? What i'm i missing?

I have tried reordering the code so that the IT type comes first but still doesn't work.

1 Answer 1

5

An important notice from the Codex:

Do pay close attention to not having your custom post type identifier exceed 20 characters though, as the post_type column in the database is currently a VARCHAR field of that length.

Please check out:

echo strlen( 'informationtechnology' );

If you have WP_DEBUG activated, then you will see this warning:

register_post_type was called incorrectly. Post type names must be between 1 and 20 characters in length. Please see Debugging in WordPress for more information. (This message was added in version 4.2.)

so this is a good reminder to use it when developing your site ;-)

This part of register_post_type() is responsible for this check, I missed it when I first skimmed through the function but confirmed it when I ran the example by @JoeyNg'ethe:

if ( empty( $post_type ) || strlen( $post_type ) > 20 ) {
    _doing_it_wrong(
        __FUNCTION__, 
        __( 'Post type names must be between 1 and 20 characters in length.' ), 
       '4.2' 
    );
    return new WP_Error( 
        'post_type_length_invalid',
        __( 'Post type names must be between 1 and 20 characters in length.' ) 
    );
}

EDIT

Look at the $post_type parameter when registering your post type

$post_type

(string) (required) Post type. (max. 20 characters, cannot contain capital letters or spaces)

Default: None

10
  • Was just about to post the same, the name is to long by one character Commented Sep 8, 2015 at 14:19
  • I have added the info from what I was going to post to your answer for info purposes ;-) Commented Sep 8, 2015 at 14:23
  • i knew it was something really simple. i overlooked the string length cheers @birgire Commented Sep 8, 2015 at 14:28
  • I wonder why it doesn't throw an error to notify the user - oh it actually does, I just checked ;-) @PieterGoosen
    – birgire
    Commented Sep 8, 2015 at 14:34
  • 1
    Old question but you've just saved my sanity! My parameter was 21 chars. Commented Mar 3, 2017 at 12:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.