0

I have a multisite setup that has two subdomains. The first is site1.example.com which contains pages page1 and page2, and the second subdomain is site2.example.com with pages page3 and page4.

I am creating a custom theme for site1.example.com in which I would like to programmatically create a menu item in site1.example.com that references page3 in site2.example.com.

What I have done so far, to programmatically create menu items that reference pages in the same subdomain, is use the function wp_update_nav_menu_item(), as follows:

$page = get_post(111); // page id of a page1, that is in the same subdomain

wp_update_nav_menu_item($menu_id, 0, array(
    'menu-item-title' => 'Page 1',
    'menu-item-object-id' => $page->ID,
    'menu-item-object' => 'page',
    'menu-item-status' => 'publish',
    'menu-item-type' => 'post_type',
));

But to reference a page in another subdomain, I had to set menu-item-type to custom and use the call as follows

wp_update_nav_menu_item($menu_id, 0, array(
    'menu-item-title' => 'Page 3',
    'menu-item-url' => 'http://site2.example.com/page3',
    'menu-item-status' => 'publish',
    'menu-item-type' => 'custom',
));

Now, my question: Is there a way to create a menu item in site1.example.com that references page3 in site2.example.com but using the first method above? (please do not ask me why not just use the second solution above, with type custom, but the reasons are too long to explain here, and I need use the first method).

I tried the following in the theme for site1.example.com:

$page = get_post(333); // page id of a page3 in the the second subdomain, which is site2.example.com, which is different than the current subdomain

wp_update_nav_menu_item($menu_id, 0, array(
    'menu-item-title' => 'Page 3',
    'menu-item-object-id' => $page->ID,
    'menu-item-object' => 'page',
    'menu-item-status' => 'publish',
    'menu-item-type' => 'post_type',
));

But doing so, resulted in this error in the debug.log file

[28-Feb-2020 20:31:48 UTC] PHP Notice:  Trying to get property 'post_parent' of non-object in /var/www/wordpress/wp-includes/nav-menu.php on line 485
[28-Feb-2020 20:31:48 UTC] PHP Notice:  Trying to get property 'post_title' of non-object in /var/www/wordpress/wp-includes/nav-menu.php on line 486

Obviously WordPress does not like it. But is there a way to fix this problem?

Thanks.

1 Answer 1

0

WP nav menus can only contain post IDs from the current site.

The reason is, each subsite has its own separate wp_posts table. When you add items by ID to a nav menu, you're saving the ID itself to the menu, and WP dynamically calculates the correct permalink for that ID every time the menu is pulled up. (It also dynamically pulls the post Title unless you have overridden it in the menu settings.) Since you could have the same ID in every subsite, if you just save a post ID into the nav menu, there's no way to tell WordPress, "Look for this ID on this other subsite." WordPress is always going to just look for that ID in the current site's post table.

However, you can switch blog IDs in PHP theme templates, so you may be able to find a different way to build your menu - one that doesn't use native WP nav menus. The switch_to_blog() function switches to pull information from a specific (numbered by ID) subsite, so you could pull links from one site, then links from a second site (and so on if you have more sites) and display them all together in a header, for example.

3
  • So, your are suggesting that I call switch_to_blog() followed by calling wp_update_nav_menu_item() and then restore_current_blog(), right? What I did is call switch_to_blog(), retrieve the page ID, then call restore_current_blog()' then wp_update_nav_menu_item()` right at the end. Maybe this is what caused things to go off. I will try your suggestion.
    – Greeso
    Commented Feb 28, 2020 at 22:20
  • Unfortunately, I do not think this would work. I will try it, but I have my suspicion. If I switch to another blog, then the menu item will be created for that other blog! Maybe I should change my approach altogether.
    – Greeso
    Commented Feb 28, 2020 at 22:26
  • No - I am saying you cannot use WP Nav Menus the way you want to, because unless you write a very unusual nav walker, WP will always look in the site it's currently on for all the page IDs in the menu. So it will only link to content on the current site. I am saying, build out the PHP logic to grab pages and switch blog IDs in a template that does not use nav menus at all.
    – WebElaine
    Commented Mar 2, 2020 at 16:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.