I have a multisite setup that has two subdomains. The first is site1.example
which contains pages page1
and page2
, and the second subdomain is site2.example
with pages page3
and page4
.
I am creating a custom theme for site1.example
in which I would like to programmatically create a menu item in site1.example
that references page3
in site2.example
.
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' => '',
'menu-item-status' => 'publish',
'menu-item-type' => 'custom',
));
Now, my question: Is there a way to create a menu item in site1.example
that references page3
in site2.example
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
:
$page = get_post(333); // page id of a page3 in the the second subdomain, which is site2.example, 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.
I have a multisite setup that has two subdomains. The first is site1.example
which contains pages page1
and page2
, and the second subdomain is site2.example
with pages page3
and page4
.
I am creating a custom theme for site1.example
in which I would like to programmatically create a menu item in site1.example
that references page3
in site2.example
.
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/page3',
'menu-item-status' => 'publish',
'menu-item-type' => 'custom',
));
Now, my question: Is there a way to create a menu item in site1.example
that references page3
in site2.example
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
:
$page = get_post(333); // page id of a page3 in the the second subdomain, which is site2.example, 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.
Share Improve this question asked Feb 28, 2020 at 21:20 GreesoGreeso 2,2347 gold badges33 silver badges57 bronze badges1 Answer
Reset to default 0WP 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.