I'm using Wordpress to create menus and I'm trying to use the object_id
to determine whether or not the menu item is active. The problem is that when I set the menu-item-object-id
manually, it is overridden by it's own ID
.
I have a function that creates the menu and then another that adds menu items to each menu:
function create_menu($menu) {
foreach($item in $key => $menu['items']) {
create_menu_item($item, $key, $menu['id']);
}
}
function create_menu_item($item, $position, $menuID) {
// i'll use a dynamic value for the object id, but not even hard coding it works
$args = [
'menu-item-position' => $position,
'menu-item-title' => (isset($item['title'])) ? $item['title'] : null,
'menu-item-classes' => (isset($item['classes'])) ? $item['classes'] : null,
'menu-item-url' => (isset($item['url'])) ? $item['url'] : null,
'menu-item-status' => (isset($item['status'])) ? $item['status'] : 'publish',
'menu-item-parent-id' => (isset($item['parent'])) ? $item['parent'] : 0,
'menu-item-object-id' => '5'
];
wp_update_nav_menu_item($menuID, 0, $args);
}
Everything seems to be working as expected except for the menu-item-object-id
property! I manually set it be object's ID but when I use wp_get_nav_menu_items()
the $post->object_id
is always the same as it's own ID, $post->ID
.
function get_menu() {
global $post;
$menu = wp_get_nav_menu_items('primary');
foreach($menu as $item) {
// $item->object_id should now be '5' but instead it is equal to $item->ID so I am unable to check if it's active
if($item->object_id == $post->ID) {
// item is active
}
}
return $menu;
}
Why is my menu-item-object-id
property being overwritten?