0
votes

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

1 Answers

0
votes

The "WordPress" way to limit the taxonomies available to a custom post type is to do so when registering the post type or when registering the taxonomy - either way.

So, in the post type that you do want this taxonomy registered, add this argument:

register_post_type(
    ...
    'taxonomies' => [ 'intranet-categories' ]
    ...
);

and / or, when registering your taxonomy, define ( in the registration ) which post-types it should be assigned to:

register_taxonomy('company', ['intranet-categories'], [...

However, it appears you may know this already (based on your code), and you still want the taxonomy associated with intranet-posts, you just don't want it to display in the admin?

So - in order to remove menu items from the dashboard, you would do something like this:

function remove_taxonomy_submenu_pages() {
    global $menu;
    // see the menu items
    var_dump( $menu );
    // after var_dump, you can see which item you want to remove, so you can remove like so:
    foreach ( $menu as $i => $item ) {
        // assuming 'intranet-post' is the menu item (which it probably isn't, find it using the var_dump info above)
        if ( 'intranet-post' == $item[2] ) {
            unset( $menu[$i] );
        }
    }
}

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