wp_get_associated_nav_menu_items

Get the menu items associated with a particular object.

Description

(array) wp_get_associated_nav_menu_items( (int) $object_id = 0, (string) $object_type = 'post_type', (string) $taxonomy = '' ); 

Returns (array)

The array of menu item IDs; empty array if none;

Parameters (3)

0. $object_id — Optional. (int)
The ID of the original object.
1. $object_type — Optional. (string) => 'post_type'
The type of object, such as taxonomy or post_type.
2. $taxonomy — Optional. (string) => ''
If $object_type is taxonomy,, $taxonomy is the name of the tax that $object_id belongs to

Usage

  1. if ( !function_exists( 'wp_get_associated_nav_menu_items' ) ) { 
  2. require_once ABSPATH . WPINC . '/nav-menu.php'; 
  3.  
  4. // The ID of the original object. 
  5. $object_id = -1; 
  6.  
  7. // The type of object, such as "taxonomy" or "post_type." 
  8. $object_type = 'post_type'; 
  9.  
  10. // If $object_type is "taxonomy", $taxonomy is the name of the tax that $object_id belongs to 
  11. $taxonomy = ''; 
  12.  
  13. // NOTICE! Understand what this does before running. 
  14. $result = wp_get_associated_nav_menu_items($object_id, $object_type, $taxonomy); 
  15.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/nav-menu.php  
  1. function wp_get_associated_nav_menu_items( $object_id = 0, $object_type = 'post_type', $taxonomy = '' ) { 
  2. $object_id = (int) $object_id; 
  3. $menu_item_ids = array(); 
  4.  
  5. $query = new WP_Query; 
  6. $menu_items = $query->query( 
  7. array( 
  8. 'meta_key' => '_menu_item_object_id',  
  9. 'meta_value' => $object_id,  
  10. 'post_status' => 'any',  
  11. 'post_type' => 'nav_menu_item',  
  12. 'posts_per_page' => -1,  
  13. ); 
  14. foreach ( (array) $menu_items as $menu_item ) { 
  15. if ( isset( $menu_item->ID ) && is_nav_menu_item( $menu_item->ID ) ) { 
  16. $menu_item_type = get_post_meta( $menu_item->ID, '_menu_item_type', true ); 
  17. if ( 
  18. 'post_type' == $object_type && 
  19. 'post_type' == $menu_item_type 
  20. ) { 
  21. $menu_item_ids[] = (int) $menu_item->ID; 
  22. } elseif ( 
  23. 'taxonomy' == $object_type && 
  24. 'taxonomy' == $menu_item_type && 
  25. get_post_meta( $menu_item->ID, '_menu_item_object', true ) == $taxonomy 
  26. ) { 
  27. $menu_item_ids[] = (int) $menu_item->ID; 
  28.  
  29. return array_unique( $menu_item_ids );