Make WordPress Core

Opened 5 weeks ago

Last modified 5 weeks ago

#63261 assigned defect (bug)

Menu items incorrectly marked as current when page and taxonomy share the same object_id

Reported by: nuadagency's profile nuadagency Owned by: audrasjb's profile audrasjb
Milestone: 6.9 Priority: normal
Severity: normal Version: 6.7.2
Component: Menus Keywords: needs-testing
Focuses: accessibility Cc:

Description (last modified by sabernhardt)

Description

If a page and a taxonomy term (e.g., product_cat) share the same numeric ID, both can be incorrectly marked as active in menus (current-menu-item, aria-current="page").

This occurs because WordPress checks only the object_id when determining the active menu item, without verifying that the object types match.

Steps to Reproduce

Create a WordPress page (e.g., "About") — assume it has ID 23.

Create a WooCommerce product category that also ends up with term_id = 23.

Add both items to a navigation menu.

Visit the product category archive (/product-category/boots/).

Observe that both menu items ("About" and "Boots") are marked as current.

Expected Result

Only the menu item representing the current object type (e.g., the product category) should be marked as current.

Actual Result

Both items are incorrectly highlighted due to ID match, even though they represent different object types.

Cause

In wp-includes/nav-menu-template.php, this comparison is made:

if ( $menu_item->object_id === $queried_object_id ) { $menu_item->current = true; }

This does not check if the object type (e.g. page, product_cat) also matches.

Proposed Fix

Enhance the condition to ensure both ID and object type match:

$queried_object_type = '';
if ( isset( $queried_object->post_type ) ) {
    $queried_object_type = $queried_object->post_type;
} elseif ( isset( $queried_object->taxonomy ) ) {
    $queried_object_type = $queried_object->taxonomy;
}
if ( (int) $menu_item->object_id === (int) $queried_object_id
    && $menu_item->object === $queried_object_type ) {
    $menu_item->current = true;
}

Environment

WordPress 6.7.2

WooCommerce 9.7.1

PHP 8.2

Notes

This issue causes confusing menu behavior and accessibility inconsistencies. It's especially common with WooCommerce where term IDs frequently overlap with page IDs.

Attachments (1)

63261.patch (1.2 KB) - added by abcd95 5 weeks ago.
There might be a better approach but this also works.

Download all attachments as: .zip

Change History (4)

#1 @sabernhardt
5 weeks ago

  • Description modified (diff)
  • Focuses coding-standards removed

#2 @audrasjb
5 weeks ago

  • Keywords needs-patch added
  • Milestone changed from Awaiting Review to 6.9
  • Owner set to audrasjb
  • Severity changed from minor to normal
  • Status changed from new to assigned

Hello and welcome to WordPress Core Trac!

I reproduced the issue on my side. Self assigning and moving to milestone 6.9.

@abcd95
5 weeks ago

There might be a better approach but this also works.

#3 @abcd95
5 weeks ago

  • Keywords needs-testing added; needs-patch removed
Note: See TracTickets for help on using tickets.