|

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

  • |

    WordPress Helper Functions for Detecting IE

    The other day I was working on a problem where I wanted to check if a website was using a specific browser (in this case IE) and version (in this case 9 or below). I came up with 2 functions that would serve an a nice, reusable check for both. These can also be extended…

  • |

    WordPress Cyber Weekend Deals 2018

    Well it’s that time of year, where there are lots of deals that I’d rather buy for myself than for others. I think nothing better encapsulates that than the digital deals going on in the WordPress /online worker space. I mean don’t get me wrong, I’d love to get a great deal on hosting. But…

  • |

    Scrubs Quotes App

    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! I don’t do too many things on a whim, but the other night when I saw a group dedicated to…

  • | |

    What Developers can learn from Home Improvement

    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! It’s no secret that I love the show Home Improvement, especially if you know me personally or follow me on…

  • |

    Quick Tip: How to Un-shrink a Sweater

    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!So this isn’t really tech related, but I’m a guy and not a very bright one at that. Last week a…

  • The way to learn WordPress is not by contributing

    has happened in WordPress this year, and what we can expect moving forward. As always, he extolled the virtues of open source and the importance of contributing. He spent several minutes on Five for the Future, a program encouraging companies who make money with WordPress to give 5% of their time to the open source project. And while I strongly recommend contributing, I don’t think it’s the path someone should take when they first learn.

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.