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: |
|
Owned by: |
|
---|---|---|---|
Milestone: | 6.9 | Priority: | normal |
Severity: | normal | Version: | 6.7.2 |
Component: | Menus | Keywords: | needs-testing |
Focuses: | accessibility | Cc: |
Description (last modified by )
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.
Hello and welcome to WordPress Core Trac!
I reproduced the issue on my side. Self assigning and moving to milestone 6.9.