|

Fix wp_nav_menu on Custom Type Archives in WordPress

Recently I was working with an issue in WordPress where the site’s menu was not showing up on an archive page for one of my custom post types. There were a few troubleshooting things I tried, including the most common recommendation, use theme_location instead of menu when referencing the menu in your theme (code after the jump):

wp_nav_menu( array(
    'theme_location' => 'main-menu-slug',
) );

However, that wasn’t working. I did find another solution that got me part of the way there, which removed the taxonomy specifications if WordPress was building the menu.

function fix_nav_menu( $query ) {
    if ( $query->get( 'post_type' ) === 'nav_menu_item' ) {
    $query->set( 'tax_query', '' );
    }
}
add_action( 'pre_get_posts', 'fix_nav_menu' );

This didn’t completely work for me because I was using a meta_key to sort my posts, so I changed the if statement to reset a couple more query variables:

if ( $query->get( 'post_type' ) === 'nav_menu_item' ) {
    $query->set( 'tax_query', '' );
    $query->set( 'meta_key', '' );
    $query->set( 'orderby', '' );
}

That did the trick! It took me quite a bit of troubleshooting, so hopefully this will help you out if you’re trying to fix wp_nav_menu on Custom Post Type Archives.

Similar Posts

  • |

    What I’m Using to Drive Memberships on Creator Courses

    Recently LearnDash announced some very exciting news in that they’ve converted/expanded Groups to allow for Memberships (and subscriptions). There’s a strong possibility I will migrate from my currently setup to this, as long as my testing shows it does what I need it to do (spoiler alert: nothing too crazy) and a migration path is…

  • | |

    Quick Tip: Download an Archive of your Facebook Data

    Note: This article was published while I was in my early 20s. I was much younger and dumber. Please don’t hold it against me. One of the perils of having a 20+ year old website!There’s a lot going on on the Internet right now. Websites getting hacked (Blizzard being the most recent), companies doing presumably…

  • Speaking: NEPABlogCon and Beyond!

    Though I haven’t officially announced it yet, I have been talking for a while about a new book I’m working on called Responsive Design with WordPress. We are a couple of months out from release but things are going well! In the time running up to the release I’ll be out there speaking quite a bit to…

  • | | |

    Winn Cards and Comics

    Note: This article was published while I was in my early 20s. I was much younger and dumber. Please don’t hold it against me. One of the perils of having a 20+ year old website! The Winn Site is a project I have been working on for a couple of months now. The guys wanted…

  • |

    Quick Tip: Custom Category Styles in WordPress

    Note: This article was published while I was in my early 20s. I was much younger and dumber. Please don’t hold it against me. One of the perils of having a 20+ year old website!Over the weekend I revamped this blog’s current theme a little bit, including adding custom styles for my Link Round-up lists….

One Comment

  1. It seems that this breaks the menu’s on all pages and anywhere the wp_nav_menu is called on the site since WP6.0.

    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: https://accelerated.local/

Comments are closed.