5

I have created a custom taxonomy that I have associated to my CPT. both appear on my dashboard, the issue is that when I add content and I want to choose a term from the list of the custom taxonomy, there is no value (no list, no checkbox...) . I am using wordpress 5.1. Here is the code added to functions.php :

function type_custom_taxonomy() {

  $labels = array(
    'name' => _x( 'Types', 'taxonomy general name' ),
    'singular_name' => _x( 'Type', 'taxonomy singular name' ),
    'menu_name' => __( 'Types' ),
  );    

  register_taxonomy('types',array('action'), array(
    'labels' => $labels,
    'hierarchical'               => true,
    'public'                     => true,
    'show_ui'                    => true,
    'show_admin_column'          => true,
    'show_in_rest'               => true,
    'show_tagcloud'              => false,



  ));
}

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

enter image description here

//CPT
function action_post_type() {
  register_post_type( 'action',
    array(
      'labels' => array(
        'name' => __( 'Actions' ),
        'singular_name' => __( 'Action' )
      ),
      'public' => true,
      'has_archive' => true,
      'show_in_rest'       => true,
      'supports'     => array('title', 'editor','thumbnail'),
      'taxonomies' => array('types')
    )
  );
}
add_action( 'init', 'action_post_type' );
3

Changing the slug of taxonomy works for me. Don't know the reason behind it but it works.

  • you mean I have to add 'rewrite' => array( 'slug' => 'type' ), It didn't work for me , Can you please show me the code – Hanane Kacemi Mar 12 at 10:05
  • No I didn't try to use rewrite but I change the slug in the register_taxonomy call like this register_taxonomy( 'kt-types', array('action'), $args). I tried it locally and it works for me. – Mehmood Ahmad Mar 12 at 10:16
  • Yes it works when I change types to action-types. Thank u so much mahmood. Can u write your answer as a solution so that I can acccept it – Hanane Kacemi Mar 13 at 16:53
  • Glad to hear that it works. I have already added my answer as a solution. Do you want me to explain it more? – Mehmood Ahmad Mar 13 at 17:08
  • sorry I didn't see it, it s clear for me thank u – Hanane Kacemi Mar 13 at 17:09
5

I face the same as your problem and solved as the following

you must add 'show_in_rest' => true, for both post_type and taxonomies int the last line of array such as

register_post_type(
'portfolio',
 array(
 'labels'              => $labels,
 'exclude_from_search' => false,
'has_archive'         => true,
'public'              => true,
'publicly_queryable' => false,
'rewrite'  => false,
'can_export'          => true,
'show_in_nav_menus'   => true,
'supports'            => array('title', 'editor', 'thumbnail', 'comments', 'page-attributes','excerpt'),
'show_in_rest' =>true,
 )
);

THE Taxonomy

register_taxonomy(
'portfoliocat',
'portfolio',
 array(
 'hierarchical'      => true,
 'show_in_nav_menus' => true,
'labels'            =>array(),
 'query_var'         => true,
 'rewrite'           => array('slug' => 'portfoliocat'),
 'show_in_rest' => true,
    )
  );
  • I had it in my CPT, but not my taxonomy registration, this solved the problem for me. – Faye Jun 26 at 18:08
0

My guess would be that it's caused by priorities...

Both of your actions are using the same priority. So if type_custom_taxonomy function is called as first one, then the action post type doesn't exist at that moment.

I would try something like this:

function action_post_type() {
  register_post_type( 'action',
    array(
      'labels' => array(
        'name' => __( 'Actions' ),
        'singular_name' => __( 'Action' )
      ),
      'public' => true,
      'has_archive' => true,
      'show_in_rest'       => true,
      'supports'     => array('title', 'editor','thumbnail'),
    )
  );

  $labels = array(
    'name' => _x( 'Types', 'taxonomy general name' ),
    'singular_name' => _x( 'Type', 'taxonomy singular name' ),
    'menu_name' => __( 'Types' ),
  );    

  register_taxonomy('types',array('action'), array(
    'labels' => $labels,
    'hierarchical'               => true,
    'public'                     => true,
    'show_ui'                    => true,
    'show_admin_column'          => true,
    'show_in_rest'               => true,
    'show_tagcloud'              => false,
  ));
}
add_action( 'init', 'action_post_type' );

This way you can be sure that the CPT is registered before the taxonomy and you can assign that taxonomy to that CPT.

  • thank u for ur response. I tried that but it didn't solve the issue. – Hanane Kacemi Mar 6 at 18:01
0

Have you added some term to taxonomy "Types"?
I think there is nothing wrong with code. Add a "Type" by clicking the link shown in screenshot 'ajouter uni nouvelle categorie' . After you add some "Types" you will be able to select.

  • yes, it is the issue that I am having, it doesn't show list of terms that I have already added. So yes I can add new term but if I want to modify my CPT I don't have the list of my terms – Hanane Kacemi Mar 8 at 14:29
  • I tried your code on my setup and it works without fail. See This. May be some other part of your code creating this issue. – Qaisar Feroz Mar 8 at 17:53
  • 1
    Yes it works if you don't use Gutenberg. but when I add 'show_in_rest' => true to my CPT to have Gutenberg Editor, it doesn't work. that is the issue – Hanane Kacemi Mar 9 at 9:08

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.