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);

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);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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