0

I'm creating my own custom post type and here is the code for this:

add_action( 'init', 'create_gallery_post_type' );
function create_gallery_post_type() {
  register_post_type( 'gallery',
    array(
      'labels' => array(
        'name' => __( 'Galleries' ),
        'singular_name' => __( 'Gallery' )
      ),
        'publicly_queryable' => true,
        'show_ui' => true,
        'query_var' => true,
        'rewrite' => true,
        'capability_type' => 'post',
        'hierarchical' => false,
        'menu_position' => null,
        'taxonomies' => array('category'),
        'supports' => array('title','editor','thumbnail')
    )
  );
}

I have another custom post type that has it's own custom category taxonomy and that is called `portfolio-category'. I'd like to use the same taxonomy for this post type since they would be the same values and I only have to enter them in one place.

I tried to make it say:

'taxonomies' => array('portfolio-category')

This didn't show me the categories link under my Gallery post type though. Any ideas? Thanks!

  • I believe you assign a taxonomy to a post type and not a post type to a taxonomy. Check your taxonomy code. – gdaniel Jan 12 '15 at 16:52
  • @gdaniel I already registered the other taxonomy. I was thinking that since it's registered I can just tell the post type to use that taxonomy. – user1048676 Jan 12 '15 at 17:19
  • @gdaniel it works both ways, shouldn't be an issue. – Rarst Jan 12 '15 at 17:38
1

What you have should work in principle. Note that taxonomies argument needs existing taxonomy, that is one that is already registered at the time you are registering CPT.

If the timing is problematic to get right during registration, you could also use register_taxonomy_for_object_type() to create association between the two separately from registration process.

| improve this answer | |
  • Thank you. Maybe that is my issue. I'm declaring this custom post type in my functions.php file within my child theme. The other custom taxonomy is being registered from within my main theme so maybe the functions.php is executing before the other taxonomy is created. I'm currently using the init hook to create this taxonomy like this add_action( 'init', 'create_gallery_post_type' );. Is there a different hook to use to create this after the files in my main theme are loaded? – user1048676 Jan 12 '15 at 17:52
  • I actually changed the hook to wp_loaded and then changed my taxonomy to the portfolio-category and it now loads succcessfully. Is the wp_loaded hook ok to use? – user1048676 Jan 12 '15 at 18:21
  • @user1048676 I would say just use init for everything and specify priorities to control order. wp_loaded is a fine hook, but it is rarely used and so a little unconventional. – Rarst Jan 12 '15 at 18:41
  • How can I specify priorities? I've never used that before. – user1048676 Jan 12 '15 at 18:42
  • @user1048676 add_action() accepts priority as an argument, it's very common/necessary way to use it :) – Rarst Jan 12 '15 at 18:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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