0

Does anyone know if there is a way to handle a custom taxonomy for the nav_menu_item built'in post type? When I assign my custom taxonomy to this post type, i don't see any change in the menu editor... It works well with posts, pages and custom post types.

Here how I've registered my custom taxonomy:

  add_action( 'init', 'my_region_taxo', 0 );
  function my_region_taxo() {
    $labels = array(
        'name'                       => _x( 'Regions', 'Taxonomy General Name', 'my-text-domain' ),
        'singular_name'              => _x( 'Region', 'Taxonomy Singular Name', 'my-text-domain' ),
        (...)
    );
    $rewrite = array(
        'slug'                       => 'region',
        'with_front'                 => true,
        'hierarchical'               => true,
    );
    $args = array(
        'labels'                     => $labels,
        'hierarchical'               => true,
        'public'                     => true,
        'show_ui'                    => true,
        'show_admin_column'          => true,
        'show_in_nav_menus'          => false,
        'show_tagcloud'              => false,
        'rewrite'                    => $rewrite,
        'show_in_rest'               => true,
    );
    register_taxonomy(
        'my_region_taxonomy',
        array(
            'post',
            'page',
            'nav_menu_item'
        ),
        $args
    );
  }
2
  • edit your question to show the code you use to define the custom taxonomy.
    – mmm
    Mar 27, 2018 at 12:09
  • question edited Mar 27, 2018 at 12:25

2 Answers 2

1

This is not how you enable adding your taxonomy terms to menus. To do that you just set show_in_nav_menus to true when registering the taxonomy. You have it set to false.

$args = array(
    'labels'                     => $labels,
    'hierarchical'               => true,
    'public'                     => true,
    'show_ui'                    => true,
    'show_admin_column'          => true,
    'show_in_nav_menus'          => true, // This
    'show_tagcloud'              => false,
    'rewrite'                    => $rewrite,
    'show_in_rest'               => true,
);

register_taxonomy(
    'my_region_taxonomy',
    array(
        'post',
        'page', // Not here
    ),
    $args
);

Your taxonomy should not actually be a taxonomy for the nav_menu_item post type.

4
  • I do't want to add my taxonomy's term in any menu, but want to manage menu items with a custom taxonomy, on the pre_get_posts hook for exemple Mar 27, 2018 at 12:34
  • 2
    Well menu items don't have an interface for adding taxonomy terms. At all. So you'd have to build that interface. Maybe there's a better approach. What are you actually trying to achieve? Mar 27, 2018 at 12:39
  • i need to filter contents by regions, it works with assigned post types except the nav_menu_item, sInce I can't assign the terms. Mar 27, 2018 at 13:21
  • I've finally found another way to handle my needs: basicaly fetching the menu items in the wp_get_nav_menu_items filter, and unset the one who are not targeting a post_type with the good term of my custom taxonomy. Mar 28, 2018 at 8:22
0

A quick way is to use the normal post edit page by enabling show_ui for the nav_menu_item post type.

add_filter('register_post_type_args', 'register_post_type_args', 10, 2);

function register_post_type_args( $args, $post_type ) {
    if ( $post_type === 'nav_menu_item' ) {
        $args['show_ui'] = true;
    }
    return $args;
}

Then you can inspect the menu to get the nav_menu_item post id, and replace it in the post edit url. I know it's not as good as being able to set the terms from the menu editor, but it's a quick way to get it working.

Your Answer

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

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