12

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.

| improve this answer | |
  • you mean I have to add 'rewrite' => array( 'slug' => 'type' ), It didn't work for me , Can you please show me the code – Hanane Mar 12 '19 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 '19 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 Mar 13 '19 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 '19 at 17:08
  • sorry I didn't see it, it s clear for me thank u – Hanane Mar 13 '19 at 17:09
26

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,
    )
  );
| improve this answer | |
  • I had it in my CPT, but not my taxonomy registration, this solved the problem for me. – Faye Jun 26 '19 at 18:08
  • 1
    This is the correct answer – user1676224 Nov 29 '19 at 15:08
  • I think maybe there was a moment where the first "selected" answer was a solution, but in 2020, it seems that 'show_in_rest' => true, is the thing that's missing if your problem is it shows in classic editor but not in Block Editor. – jerclarke Aug 28 at 18:36
2

I can reproduce this as well. Adding show_in_rest in register_taxonomy function, as suggested by many, would normally be proper answer, but not full answer in your case. This is because rest endpoint types is already in use by Wordpress itself. https://example.com/wp-json/wp/v2/types will return registered post types and therefore Gutenberg does not understand it. See https://developer.wordpress.org/rest-api/reference/post-types/

Thus, changing slug is probably the best way to go. If your site is live, then you might want to consider some rewrite rules so SEO won't take hit.

Still, even if you change the slug, you need to add show_in_rest as well when using Gutenberg.

| improve this answer | |
  • +1 for a well diagnosed and informative answer. – dj.cowan Mar 15 at 7:37
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.

| improve this answer | |
  • thank u for ur response. I tried that but it didn't solve the issue. – Hanane Mar 6 '19 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.

| improve this answer | |
  • 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 Mar 8 '19 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 '19 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 Mar 9 '19 at 9:08
0

I can reproduce the same issue.

He sees the Tab, but no entries. So the show_in_rest is not the solution and was already setup properly.

The issue is and empty Rest-API Response. This is no happening on a clean install.

Go (on a local instance) through your plugins, deactivate one by one. Look at http://domain.test/wp-json/wp/v2/taxonomyname in a separate window to see if it affects your output.

In my case it was the "User Access Manager" Plugin with the default setting "Taxonomies settings"->Hide empty Taxonomies option turned to yes!

In this case Select your Taxonomy under "Object type" and put the option to No.

| improve this answer | |

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.