|

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

  • |

    Gloss WordPress Plugin

    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!Today, I officially released my first public WordPress plugin, Gloss. Gloss is a dictionary/glossary management plugin I decided to build which…

  • |

    Add Attachments to WordPress Search Results

    I feel like this has to have been done a lot, and there are great plugins out there for it, but if you’re just looking to add a quick function to your theme (or a really simple plugin) yourself, here’s how to modify WordPress’ search query to include attachments (like images). function attachment_search( $query ) {…

  • Writing a User log-in

    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!Here is the user login code I have been promising. It’s an image so it looks nice. You can download it…

  • Caring Enough to Give Back to Your Community

    People who know me probably know a few tenets of my character: I can get very passionate about things, I like doing stuff (to the point where I say yes too much), and I give back where I can. Over the weekend, I hosted an event called the NEPA Hackathon and it was a lot of fun….

  • Answer Questions that are Being Asked

    Perhaps you’ve heard of the Socratic Method? At The University of Scranton, we loved good old Socrates, so we studied him and his methods a lot. The general idea, derived from how he would debate people, is to ask a series of questions until you get to the truth of the matter. He would often…

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.