On this page
wp_get_associated_nav_menu_items( int $object_id, string $object_type = 'post_type', string $taxonomy = '' ): int[]
Returns the menu items associated with a particular object.
Parameters
$object_idint Optional-
The ID of the original object. Default 0.
$object_typestring Optional-
The type of object, such as
'post_type'or'taxonomy'.
Default'post_type'.Default:
'post_type' $taxonomystring Optional-
If $object_type is
'taxonomy', $taxonomy is the name of the tax that $object_id belongs to.Default:
''
Return
int[] The array of menu item IDs; empty array if none.
Source
File: wp-includes/nav-menu.php. View all references
function wp_get_associated_nav_menu_items( $object_id = 0, $object_type = 'post_type', $taxonomy = '' ) {
$object_id = (int) $object_id;
$menu_item_ids = array();
$query = new WP_Query;
$menu_items = $query->query(
array(
'meta_key' => '_menu_item_object_id',
'meta_value' => $object_id,
'post_status' => 'any',
'post_type' => 'nav_menu_item',
'posts_per_page' => -1,
)
);
foreach ( (array) $menu_items as $menu_item ) {
if ( isset( $menu_item->ID ) && is_nav_menu_item( $menu_item->ID ) ) {
$menu_item_type = get_post_meta( $menu_item->ID, '_menu_item_type', true );
if (
'post_type' === $object_type &&
'post_type' === $menu_item_type
) {
$menu_item_ids[] = (int) $menu_item->ID;
} elseif (
'taxonomy' === $object_type &&
'taxonomy' === $menu_item_type &&
get_post_meta( $menu_item->ID, '_menu_item_object', true ) == $taxonomy
) {
$menu_item_ids[] = (int) $menu_item->ID;
}
}
}
return array_unique( $menu_item_ids );
}
Related
Uses
| Uses | Description |
|---|---|
| WP_Query::__construct() wp-includes/class-wp-query.php | Constructor. |
| is_nav_menu_item() wp-includes/nav-menu.php | Determines whether the given ID is a nav menu item. |
| get_post_meta() wp-includes/post.php | Retrieves a post meta field for the given post ID. |
Used By
| Used By | Description |
|---|---|
| _wp_delete_post_menu_item() wp-includes/nav-menu.php | Callback for handling a menu item when its original object is deleted. |
| _wp_delete_tax_menu_item() wp-includes/nav-menu.php | Serves as a callback for handling a menu item when its original object is deleted. |
Changelog
| Version | Description |
|---|---|
| 3.0.0 | Introduced. |
© 2003–2022 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/wp_get_associated_nav_menu_items