fix: resolve nav menu item validation to check for published status#9192
fix: resolve nav menu item validation to check for published status#9192kushagra-goyal-14 wants to merge 3 commits intoWordPress:trunkfrom
Conversation
Test using WordPress PlaygroundThe changes in this pull request can previewed and tested using a WordPress Playground instance. WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser. Some things to be aware of
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
src/wp-includes/nav-menu.php
Outdated
| } | ||
|
|
||
| if ( 'trash' === get_post_status( $menu_item->object_id ) ) { | ||
| if ( 'publish' !== get_post_status( $menu_item->object_id ) ) { |
There was a problem hiding this comment.
This will need to account for custom post statuses that are public.
It should probably check is_post_status_viewable( get_post_status( $menu_item->object_id ) )
Menu items that get unpublished still appear.
Overview
This pull request updates the logic for rendering menu items in WordPress navigation menus. It ensures that only items linked to published posts or pages are displayed on the frontend, improving consistency and preventing dead links.
Previously, menu items tied to
draft,pending, andprivatecontent would still appear on the frontend, leading to broken or inaccessible navigation links.Trac Ticket: #13822
Issue Fixed
Menu Items Linked to Unpublished Posts Still Show Publicly
Problem:
Menu items remain visible on the frontend even when the linked post is no longer published (e.g., moved to trash or reverted to draft).
Solution:
Updated the condition to check for is_post_viewable and if private post then show based on user capability
Code Changes
Previous behavior (only hiding trashed items)
Updated behavior (hides all non-published items)
This ensures the menu item is only considered valid when the linked object is in the
'publish'state.Admin Behavior
(Draft)(Pending)(Trash)(Private)“Click Save Menu to make unsaved menu items public.”
already informs users that certain items won't be visible on the live site.
No changes were made to the admin UI — it already handles this use case effectively.
Testing Instructions
Test Case 1: Hide Non-Published Menu Items
Expected Results
Test Case 2: Private Posts - Anonymous Users
Objective: Verify private posts are hidden from non-authenticated users
Steps:
Expected Results:
Test Case 3: Private Posts - Subscribers
Objective: Verify private posts are hidden from users without read_private_posts capability
Steps:
Expected Results:
Test Case 4: Private Posts - Admins/Editors
Objective: Verify private posts are visible to users with read_private_posts capability
Steps:
Expected Results:
Test Case 5: Custom Public Post Status (Backward compatbility)
Objective: Verify custom public post statuses work correctly
Prerequisites: Register a custom public post status
Steps:
Expected Results: