Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion src/wp-includes/nav-menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -1049,12 +1049,19 @@ function wp_setup_nav_menu_item( $menu_item ) {
* Default 'post_type'.
* @param string $taxonomy Optional. If $object_type is 'taxonomy', $taxonomy is the name
* of the tax that $object_id belongs to. Default empty.
* @param int $menu_id Optional. The ID of the menu. Default 0.
* @return int[] The array of menu item IDs; empty array if none.
*/
function wp_get_associated_nav_menu_items( $object_id = 0, $object_type = 'post_type', $taxonomy = '' ) {
function wp_get_associated_nav_menu_items( $object_id = 0, $object_type = 'post_type', $taxonomy = '', $menu_id = 0 ) {
$object_id = (int) $object_id;
$menu_id = (int) $menu_id;
$menu_item_ids = array();

// Bail out if the menu ID does not exist.
if ( 0 !== $menu_id && ! term_exists( $menu_id, 'nav_menu' ) ) {
return $menu_item_ids;
}

$query = new WP_Query();
$menu_items = $query->query(
array(
Expand All @@ -1067,6 +1074,14 @@ function wp_get_associated_nav_menu_items( $object_id = 0, $object_type = 'post_
);
foreach ( (array) $menu_items as $menu_item ) {
if ( isset( $menu_item->ID ) && is_nav_menu_item( $menu_item->ID ) ) {
// If a menu ID is given, only consider menu items that belong to that menu.
if ( 0 !== $menu_id ) {
$menu_item_terms = wp_get_post_terms( $menu_item->ID, 'nav_menu' );
if ( ! in_array( $menu_id, wp_list_pluck( $menu_item_terms, 'term_id' ), true ) ) {
continue;
}
}

$menu_item_type = get_post_meta( $menu_item->ID, '_menu_item_type', true );
if (
'post_type' === $object_type &&
Expand Down
42 changes: 42 additions & 0 deletions tests/phpunit/tests/post/nav-menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,48 @@ public function test_wp_get_associated_nav_menu_items() {
$this->assertSameSets( array(), $page_items );
}

/**
* @ticket 14439
*/
public function test_wp_get_associated_nav_menu_items_menu_id() {
$menu_2_id = wp_create_nav_menu( 'bar' );
$post_id = self::factory()->post->create();

$menu_1_item = wp_update_nav_menu_item(
$this->menu_id,
0,
array(
'menu-item-type' => 'post_type',
'menu-item-object' => 'post',
'menu-item-object-id' => $post_id,
'menu-item-status' => 'publish',
)
);

$menu_2_item = wp_update_nav_menu_item(
$menu_2_id,
0,
array(
'menu-item-type' => 'post_type',
'menu-item-object' => 'post',
'menu-item-object-id' => $post_id,
'menu-item-status' => 'publish',
)
);

// Without a menu ID, menu items from all menus are returned.
$items = wp_get_associated_nav_menu_items( $post_id );
$this->assertSameSets( array( $menu_1_item, $menu_2_item ), $items );

// With a menu ID, only the menu items of that menu are returned.
$items = wp_get_associated_nav_menu_items( $post_id, 'post_type', '', $menu_2_id );
$this->assertSameSets( array( $menu_2_item ), $items );

// An invalid menu ID returns an empty array.
$items = wp_get_associated_nav_menu_items( $post_id, 'post_type', '', 999999 );
$this->assertSameSets( array(), $items );
}

/**
* @ticket 27113
*/
Expand Down
Loading