0

I have made custom taxonomies that I am using for few different post types:

<?php
add_action('init', function() {
    register_post_type('intranet-categories', [
        'labels' => [
            'name'                  => 'Kategorier',
            'singular_name'         => 'Kategorier',
            'menu_name'             => 'Kategorier',
            'name_admin_bar'        => 'Kategorier',
            'add_new'               => 'Legg til ny',
            'add_new_item'          => 'Ny Kategori',
            'new_item'              => 'Ny Kategori',
            'edit_item'             => 'Rediger Kategori',
            'view_item'             => 'Vis Kategori',
            'all_items'             => 'Alle Kategorier',
            'search_items'          => 'Søk',
            'parent_item_colon'     => 'Forelder',
            'not_found'             => 'Fant ingen Kategori.',
            'not_found_in_trash'    => 'Fant ingen Kategori i søppelkassen.',
        ],
        'description'           => 'Kategori',
        'public'                => true,
        'publicly_queryable'    => true,
        'show_ui'               => true,
        'show_in_menu'          => true,
        'query_var'             => true,
        'rewrite'               => ['slug' => 'intranet-categories'],
        'capability_type'       => 'post',
        'has_archive'           => true,
        'hierarchical'          => false,
        'menu_position'         => null,
        'menu_icon'             => 'dashicons-editor-ol',
        'supports'              => ['title', 'editor', 'excerpt', 'thumbnail'],
        'capability_type' => 'custom_post_type',
        'capabilities' => ['create_posts' => false],
    ]);

    register_taxonomy('department', ['intranet-categories', 'intranet-post'], [
        'labels' => [
            'name'              => 'Avdeling',
            'singular_name'     => 'Avdeling',
            'search_items'      => 'Søk',
            'all_items'         => 'Alle avdelinger',
            'parent_item'       => 'Forelder',
            'parent_item_colon' => 'Forelder',
            'edit_item'         => 'Rediger avdeling',
            'update_item'       => 'Oppdater avdeling',
            'add_new_item'      => 'Opprett avdeling',
            'new_item_name'     => 'Ny avdeling',
            'menu_name'         => 'Avdelinger',
        ],
        'hierarchical'      => true,
        'show_ui'           => true,
        'show_admin_column' => true,
        'query_var'         => true,
        'rewrite'           => ['slug' => 'avdeling'],
    ]);

    register_taxonomy('region', ['intranet-categories', 'intranet-post'], [
        'labels' => [
            'name'              => 'Region',
            'singular_name'     => 'Region',
            'search_items'      => 'Søk',
            'all_items'         => 'Alle region',
            'parent_item'       => 'Forelder',
            'parent_item_colon' => 'Forelder',
            'edit_item'         => 'Rediger Region',
            'update_item'       => 'Oppdater Region',
            'add_new_item'      => 'Opprett Region',
            'new_item_name'     => 'Ny Region',
            'menu_name'         => 'Regioner',
        ],
        'hierarchical'      => true,
        'show_ui'           => true,
        'show_admin_column' => true,
        'query_var'         => true,
        'rewrite'           => ['slug' => 'avdeling'],
    ]);

    register_taxonomy('industry', ['intranet-categories', 'intranet-post'], [
        'labels' => [
            'name'              => 'Bransje',
            'singular_name'     => 'Bransje',
            'search_items'      => 'Søk',
            'all_items'         => 'Alle bransjer',
            'parent_item'       => 'Forelder',
            'parent_item_colon' => 'Forelder',
            'edit_item'         => 'Rediger bransje',
            'update_item'       => 'Oppdater bransje',
            'add_new_item'      => 'Opprett bransje',
            'new_item_name'     => 'Ny bransje',
            'menu_name'         => 'Bransjer',
        ],
        'hierarchical'      => true,
        'show_ui'           => true,
        'show_admin_column' => true,
        'query_var'         => true,
            'rewrite' => ['slug' => 'bransje'],
        ]
    );

    register_taxonomy('role', ['intranet-categories', 'intranet-post'], [
        'labels' => [
            'name'              => 'Rolle',
            'singular_name'     => 'Rolle',
            'search_items'      => 'Søk',
            'all_items'         => 'Alle roller',
            'parent_item'       => 'Forelder',
            'parent_item_colon' => 'Forelder',
            'edit_item'         => 'Rediger rolle',
            'update_item'       => 'Oppdater rolle',
            'add_new_item'      => 'Opprett rolle',
            'new_item_name'     => 'Ny rolle',
            'menu_name'         => 'Roller',
        ],
        'hierarchical'      => true,
        'show_ui'           => true,
        'show_admin_column' => true,
        'query_var'         => true,
            'rewrite' => ['slug' => 'rolle'],
        ]
    );

    register_taxonomy('company', ['intranet-categories', 'intranet-post'], [
        'labels' => [
            'name'              => 'Selskap',
            'singular_name'     => 'Selskap',
            'search_items'      => 'Søk',
            'all_items'         => 'Alle selskaper',
            'parent_item'       => 'Forelder',
            'parent_item_colon' => 'Forelder',
            'edit_item'         => 'Rediger selskap',
            'update_item'       => 'Oppdater selskap',
            'add_new_item'      => 'Opprett selskap',
            'new_item_name'     => 'Ny selskap',
            'menu_name'         => 'Selskaper',
        ],
        'hierarchical'      => true,
        'show_ui'           => true,
        'show_admin_column' => true,
        'query_var'         => true,
            'rewrite' => ['slug' => 'selskap'],
        ]
    );
});

What I would like to do is to hide the created custom taxonomies in the admin menu for intranet-post type, and only have them visible in the admin menu for intranet-categories, since I only want to edit those custom taxonomies and add terms to them from intranet-categories post type. I have tried something to do that like this:

function remove_taxonomy_submenu_pages() {
    $categories = get_object_taxonomies('intranet-categories');
    foreach($categories as $category) {
        remove_menu_page( 'edit.php', 'edit-tags.php?taxonomy=' . $category . '&post_type=intranet-post' );
    }
}

add_action( 'admin_menu', 'remove_taxonomy_submenu_pages', 20);

But, that didn't work, what is the correct way to do this?

1

Referencing register_taxonomy() https://codex.wordpress.org/Function_Reference/register_taxonomy

show_in_menu Where to show the taxonomy in the admin menu. show_ui must be true.

Default: value of show_ui argument

  • 'false' - do not display in the admin menu

  • 'true' - show as a submenu of associated object types

0

You could use a plugin such as : https://en-gb.wordpress.org/plugins/admin-menu-editor/

Which enables you to remove menu items for different role groups. Used along with a user role editor plugin you can pretty much narrow down everything for a nice clean admin panel for a certain role

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.