wp_nav_menu_post_type_meta_boxes()WP 3.0.0

Creates a meta-box for any post type in the accordion on the menu creation page.

Works based on the function get_post_types(), with the parameter 'show_in_nav_menus' => true.

The function is used by the engine and is generally not intended for use elsewhere.

To display similar meta-boxes for taxonomies, wp_nav_menu_taxonomy_meta_boxes() is used.

Each obtained post type object is passed through the hook-filter nav_menu_meta_box_object, where you can change, for example, the title of the meta-box or completely cancel its output in the accordion.

The meta-box is added by the function add_meta_box() according to the following template:

add_meta_box( "add-post-type-{$id}",
	$post_type->labels->name,
	'wp_nav_menu_item_post_type_meta_box',
	'nav-menus',
	'side',
	$priority,
	$post_type
);

Where:

  • $post_type->name - the name of the post, for example post, page, and so on.
  • $id = $post_type->name.
  • $priority = ( 'page' == $post_type->name ? 'core' : 'default' ).
  • $post_type - the post type object.
Hooks from the function

Returns

null. Nothing (null).

Usage

wp_nav_menu_post_type_meta_boxes();

Examples

0

#1 Used in the WP core

See the wp_nav_menu_setup() function code for an example of how to use it.

See the file /wp-admin/nav-menus.php for an example of how to use it.

Changelog

Since 3.0.0 Introduced.

wp_nav_menu_post_type_meta_boxes() code WP 6.9

function wp_nav_menu_post_type_meta_boxes() {
	$post_types = get_post_types( array( 'show_in_nav_menus' => true ), 'object' );

	if ( ! $post_types ) {
		return;
	}

	foreach ( $post_types as $post_type ) {
		/**
		 * Filters whether a menu items meta box will be added for the current
		 * object type.
		 *
		 * If a falsey value is returned instead of an object, the menu items
		 * meta box for the current meta box object will not be added.
		 *
		 * @since 3.0.0
		 *
		 * @param WP_Post_Type|false $post_type The current object to add a menu items
		 *                                      meta box for.
		 */
		$post_type = apply_filters( 'nav_menu_meta_box_object', $post_type );

		if ( $post_type ) {
			$id = $post_type->name;
			// Give pages a higher priority.
			$priority = ( 'page' === $post_type->name ? 'core' : 'default' );
			add_meta_box(
				"add-post-type-{$id}",
				$post_type->labels->name,
				'wp_nav_menu_item_post_type_meta_box',
				'nav-menus',
				'side',
				$priority,
				$post_type
			);
		}
	}
}