switch_to_blog() and wp_nav_menu() not functioning properly

I have an MU plugin active on the network with this small function to query menu 4 from the main site in my network. On the main site in the network (1) things work as expected.

On the sub sites in the network (2,3,4) no menu gets displayed (since I have the fallback set to false, and no menu is found).

I'm wondering if I'm doing something wrong here, or if something larger is going wrong.

/**
*   Multisite menu
*/
function wp_multisite_nav_menu() {
    global $blog_id;
    $args =  array(
        'menu'            = 4,
        'fallback_cb' = false,
        'menu_class'      = 'main-nav',
        'depth'           = 0,
    );
    if ( ! is_multisite() || 1 == $blog_id ) {
        wp_nav_menu( $args );
        return;
    }
    // switch to blog, and query
    switch_to_blog( 1 );
    wp_nav_menu( $args );  
    // switch back to original blog
    restore_current_blog();
}

I can see that switch_to_blog is properly switching, as I can retrieve data from the main site using get_bloginfo( 'name' ); and get_the_title( 123 ); without any issues.

Alternatively, if this isn't the best way to go about it - I'm up for additional solutions.

Topic switch-to-blog multisite menus Wordpress

Category Web


After a bit of debugging, I was able to narrow this down to the following:

The menu that I had created to display across each site in the network was comprised of Custom post types that were not registered on any other subsite, only the main site (1).

For whatever reason the custom post type must be registered on each site in the network where you will displaying the menu (2,3 etc.).

This can be done in the same way that it was done on the top level parent site (1), but alter the show_ui to false inside of the register_post_type call, to prevent the custom post type from appearing in the child site admin menu.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.