Opened 2 years ago
Last modified 2 years ago
#56154 new defect (bug)
pre_get_posts fix to show menu on CPT Archives broken in 6.0
Reported by: | joshpaynedesigns | Owned by: | |
---|---|---|---|
Milestone: | Awaiting Review | Priority: | normal |
Severity: | normal | Version: | 6.0 |
Component: | Menus | Keywords: | has-testing-info dev-feedback |
Focuses: | Cc: |
Description (last modified by )
Before WP 6.0 we have had to add this pre_get_posts fix to show the wp_nav_menu on custom post type archive and taxonomy pages: https://casabona.org/2015/03/fix-wp_nav_menu-custom-type-archives-wordpress/
function fix_nav_menu( $query ) { if ( $query->get( 'post_type' ) === 'nav_menu_item' ) { $query->set( 'tax_query', '' ); $query->set( 'meta_key', '' ); $query->set( 'orderby', '' ); } } add_action( 'pre_get_posts', 'fix_nav_menu' );
Here is an example of the menu not showing on the CPT taxonomy page: Link to production website removed, development example link is below.]
There is a bug in 6.0 that when this is in the functions.php file the WP menu adds every menu item and other wp nav menus to each wp nav on both the backend in the menu editor and the frontend of the site. Makes for a very large menu anywhere a wp menu is called.
Example of the broken menu: https://acbase.wpengine.com/
Attachments (1)
Change History (8)
#2
@
2 years ago
- Keywords has-testing-info added
Testing Instructions
Steps to Reproduce
- Activate a classic theme.
- Add the following snippet to the classic theme's
functions.php
file:<?php function fix_nav_menu( $query ) { if ( $query->get( 'post_type' ) === 'nav_menu_item' ) { $query->set( 'tax_query', '' ); $query->set( 'meta_key', '' ); $query->set( 'orderby', '' ); } } add_action( 'pre_get_posts', 'fix_nav_menu' );
- Navigate to
Appearance > Menus
. If there aren't any existing menus, create one, add "Sample Page" to it and save. - Create a second menu called "Test menu".
- Add menu items to "Test menu" and click the
Save Menu
button. - 🐞 Once the menu has saved, menu items from the other menu are now displayed in "Test menu".
- Switch to the other menu.
- 🐞 Menu items from "Test menu" are now displayed in this menu.
Expected Results
- ❌ Menu items from other menus are displayed.
#3
@
2 years ago
- Keywords dev-feedback added
Reproduction Report
Environment
- Server: Apache (Linux)
- WordPress: 6.1-alpha-53344-src
- Browser: Chrome 103.0.0.0
- OS: Windows 10
- Theme: Twenty Twenty-One
- Plugins: None activated
Actual Results
- ✅ Menu items from other menus are displayed. (reproduced).
Additional Notes
- Introduced in [53010]. @spacedmonkey @peterwilsoncc What are your thoughts on this?
#6
@
2 years ago
The issue here is this line
$query->set( 'tax_query', '' );
Remove this line should fix the issue. But in trust, I am not sure of what the original issue is.
I strongly recommend using wp_get_nav_menu_items
where possible to avoid issues like this.
#7
@
2 years ago
So removing the tax_query line did fix my issue but since I am sorting my posts by meta_key it wasn't needed. I guess the tax_query isn't needed anymore. If I remove the function completely the menu still disappears on my tax page that's sorted by meta_key. I'm not sure how the wp_get_nav_menu_items would fix this issue but for now removing the tax_query line worked.
Hi @joshpaynedesigns, welcome to Trac and thanks for opening this ticket!
I'll add some testing instructions and an issue reproduction report to help move this ticket forward.