0

I have a function that programmatically creates menu items in Wordpress. I am able to create menu items to individual pages and posts, but how can I create a menu item for a custom post type archive? Specifically, what are the arguments passed to wp_update_nav_menu_item()?

$args = []; // what goes in this array?

wp_update_nav_menu_item($menuID, 0, $args);
Michael Lynch
  • 2,682
  • 3
  • 31
  • 59

1 Answers1

0

After searching through the core Wordpress files, I found the necessary arguments. The key is setting menu-item-object to be the slug of your post type and the menu-item-type to be post_type_archive.

$args = [
    'menu-item-position' => $position,
    'menu-item-status' => 'publish',
    'menu-item-parent-id' => 0,
    'menu-item-object' => 'my-post-type',
    'menu-item-type' => 'post_type_archive'
];

wp_update_nav_menu_item($menuID, 0, $args);
Michael Lynch
  • 2,682
  • 3
  • 31
  • 59