The nav_menu_meta_box_object filter allows you to control whether a menu items meta box will be added for the current object type in WordPress.
Usage
add_filter('nav_menu_meta_box_object', 'your_custom_function', 10, 1); function your_custom_function($post_type) { // your custom code here return $post_type; }
Parameters
$post_type
(WP_Post_Type|false) – The current object to add a menu items meta box for.
Examples
Prevent a specific post type from showing in the menu meta box
add_filter('nav_menu_meta_box_object', 'exclude_post_type_from_menu', 10, 1); function exclude_post_type_from_menu($post_type) { if ('your_post_type' == $post_type->name) { return false; } return $post_type; }
This code snippet prevents the custom post type your_post_type
from appearing in the menu items meta box.
Prevent all custom post types from showing in the menu meta box
add_filter('nav_menu_meta_box_object', 'exclude_custom_post_types_from_menu', 10, 1); function exclude_custom_post_types_from_menu($post_type) { if ('post' != $post_type->name && 'page' != $post_type->name) { return false; } return $post_type; }
This code snippet prevents all custom post types from appearing in the menu items meta box, showing only posts and pages.
Allow only specific custom post types in the menu meta box
add_filter('nav_menu_meta_box_object', 'allow_specific_post_types_in_menu', 10, 1); function allow_specific_post_types_in_menu($post_type) { $allowed_post_types = array('post', 'page', 'your_custom_post_type'); if (!in_array($post_type->name, $allowed_post_types)) { return false; } return $post_type; }
This code snippet allows only specific post types (in this case, posts, pages, and your_custom_post_type
) to appear in the menu items meta box.
Show only public post types in the menu meta box
add_filter('nav_menu_meta_box_object', 'show_only_public_post_types_in_menu', 10, 1); function show_only_public_post_types_in_menu($post_type) { if (!$post_type->public) { return false; } return $post_type; }
This code snippet ensures that only public post types will be shown in the menu items meta box.
Prevent a specific post type from showing in the menu meta box for non-admin users
add_filter('nav_menu_meta_box_object', 'restrict_post_type_from_non_admin_users', 10, 1); function restrict_post_type_from_non_admin_users($post_type) { if ('your_post_type' == $post_type->name && !current_user_can('manage_options')) { return false; } return $post_type; }
This code snippet prevents the custom post type your_post_type
from appearing in the menu items meta box for non-admin users.