Make WordPress Core

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's profile 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 costdev)

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)

Screen Shot 2022-07-01 at 3.48.35 PM.png (1.1 MB) - added by joshpaynedesigns 2 years ago.

Download all attachments as: .zip

Change History (8)

#1 @costdev
2 years ago

  • Description modified (diff)

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.

Last edited 2 years ago by costdev (previous) (diff)

#2 @costdev
2 years ago

  • Keywords has-testing-info added

Testing Instructions

Steps to Reproduce

  1. Activate a classic theme.
  2. 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' );
    
  3. Navigate to Appearance > Menus. If there aren't any existing menus, create one, add "Sample Page" to it and save.
  4. Create a second menu called "Test menu".
  5. Add menu items to "Test menu" and click the Save Menu button.
  6. 🐞 Once the menu has saved, menu items from the other menu are now displayed in "Test menu".
  7. Switch to the other menu.
  8. 🐞 Menu items from "Test menu" are now displayed in this menu.

Expected Results

  • ❌ Menu items from other menus are displayed.

#3 @costdev
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?

#4 @joshpaynedesigns
2 years ago

@costdev Thanks for your help on this.

#5 @costdev
2 years ago

@joshpaynedesigns My pleasure! 🙂

#6 @spacedmonkey
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 @joshpaynedesigns
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.

Last edited 2 years ago by joshpaynedesigns (previous) (diff)
Note: See TracTickets for help on using tickets.