Add a Post Type Archive to a WordPress Menu

To add a post type archive to a menu in WordPress, most people go to Appearance > Menus, open the box for their custom post type and only find single posts. The archive link (/projects/, /books/) seems to be missing, so they paste it in as a Custom Link. That hard-coded URL breaks when the slug or domain changes. The menu item also never gets an active class.

This snippet adds a Post Type Archives box to the menu screen. It lists every public custom post type that has an archive. The items it adds are real post_type_archive menu items, not custom links, so WordPress builds the URL from the current permalinks. A second filter marks the archive item as the active parent while a visitor reads one of that post type’s single posts.

The snippet

Put it in a small plugin, an mu-plugin, your theme’s functions.php or a Scripts Organizer PHP block. It’s one file, with no assets and no build step.

<?php
/**
 * Plugin Name: DPC Post Type Archives in Menus
 * Description: Adds a "Post Type Archives" box to Appearance > Menus and highlights the archive item on single posts.
 */

add_action( 'admin_head-nav-menus.php', 'dpc_add_archive_menu_meta_box' );
add_filter( 'nav_menu_css_class', 'dpc_archive_menu_item_classes', 10, 2 );

function dpc_add_archive_menu_meta_box(): void {
	add_meta_box(
		'dpc-post-type-archives',
		__( 'Post Type Archives', 'dpc' ),
		'dpc_render_archive_menu_meta_box',
		'nav-menus',
		'side',
		'default'
	);
}

/**
 * Public custom post types that have an archive page.
 *
 * @return WP_Post_Type[]
 */
function dpc_get_archive_post_types(): array {
	$post_types = get_post_types(
		array(
			'public'   => true,
			'_builtin' => false,
		),
		'objects'
	);

	$post_types = array_filter(
		$post_types,
		static fn( WP_Post_Type $post_type ): bool => ! empty( $post_type->has_archive )
	);

	return (array) apply_filters( 'dpc_menu_archive_post_types', $post_types );
}

function dpc_render_archive_menu_meta_box(): void {
	global $nav_menu_selected_id;

	if ( ! class_exists( 'Walker_Nav_Menu_Checklist' ) || ! function_exists( 'wp_nav_menu_disabled_check' ) ) {
		require_once ABSPATH . 'wp-admin/includes/nav-menu.php';
	}

	$post_types = dpc_get_archive_post_types();

	if ( ! $post_types ) {
		echo '<p>' . esc_html__( 'No public post type has an archive (has_archive) yet.', 'dpc' ) . '</p>';
		return;
	}

	$items = array();

	foreach ( $post_types as $post_type ) {
		// Negative placeholder IDs: the same scheme core uses for unsaved menu items.
		$placeholder = ( isset( $GLOBALS['_nav_menu_placeholder'] ) && $GLOBALS['_nav_menu_placeholder'] < 0 )
			? (int) $GLOBALS['_nav_menu_placeholder'] - 1
			: -1;

		$GLOBALS['_nav_menu_placeholder'] = $placeholder;

		$items[] = (object) array(
			'ID'               => $placeholder,
			'db_id'            => 0,
			'object_id'        => $placeholder,
			'object'           => $post_type->name,
			'type'             => 'post_type_archive',
			'post_type'        => 'nav_menu_item',
			'post_title'       => $post_type->labels->archives,
			'post_content'     => '',
			'post_excerpt'     => '',
			'menu_item_parent' => 0,
			'url'              => get_post_type_archive_link( $post_type->name ),
			'target'           => '',
			'attr_title'       => '',
			'classes'          => array(),
			'xfn'              => '',
		);
	}

	$items = array_map( 'wp_setup_nav_menu_item', $items );
	$box   = 'dpc-post-type-archives-box';
	?>
	<div id="<?php echo esc_attr( $box ); ?>" class="posttypediv">
		<div class="tabs-panel tabs-panel-active">
			<ul class="categorychecklist form-no-clear">
				<?php
				// Core walker: prints the same escaped checkbox markup as the built-in boxes.
				echo walk_nav_menu_tree(
					$items,
					0,
					(object) array( 'walker' => new Walker_Nav_Menu_Checklist() )
				);
				?>
			</ul>
		</div>
		<p class="button-controls wp-clearfix" data-items-type="<?php echo esc_attr( $box ); ?>">
			<span class="add-to-menu">
				<input type="submit" class="button submit-add-to-menu right"
					value="<?php esc_attr_e( 'Add to Menu', 'dpc' ); ?>"
					name="add-dpc-post-type-archives-item"
					id="<?php echo esc_attr( 'submit-' . $box ); ?>"
					<?php wp_nav_menu_disabled_check( $nav_menu_selected_id ); ?> />
				<span class="spinner"></span>
			</span>
		</p>
	</div>
	<?php
}

/**
 * Mark the archive menu item as the active parent while viewing one of its posts.
 */
function dpc_archive_menu_item_classes( $classes, $menu_item ): array {
	$classes = (array) $classes;

	if ( 'post_type_archive' !== ( $menu_item->type ?? '' ) || empty( $menu_item->object ) ) {
		return $classes;
	}

	if ( is_singular( $menu_item->object ) ) {
		$classes[] = 'current-menu-ancestor';
		$classes[] = 'dpc-current-archive';
	}

	return array_values( array_unique( $classes ) );
}

How it works

  • admin_head-nav-menus.php runs only on Appearance > Menus. By then core has added its own boxes and set which ones a new user sees, so this box shows up straight away instead of being hidden in Screen Options.
  • dpc_get_archive_post_types() keeps the public, non-built-in post types whose has_archive is set. A post type without an archive has no URL to link to.
  • Each archive becomes a placeholder menu item of type post_type_archive, the same item type core uses. wp_setup_nav_menu_item() fills in the label and URL. Core’s Walker_Nav_Menu_Checklist prints the checkboxes, so the Add to Menu button uses core’s own JavaScript and its nonce-checked add-menu-item AJAX request. You don’t need a custom save handler, and only users who can edit_theme_options can open this screen.
  • WordPress already gives a post_type_archive item current-menu-item on the archive page itself, but not on a single post. dpc_archive_menu_item_classes() adds current-menu-ancestor and dpc-current-archive there, so your “Projects” menu item stays highlighted while someone reads one project.

Customise it

Use the dpc_menu_archive_post_types filter to drop a post type from the box, or to add one that the default query skips:

add_filter( 'dpc_menu_archive_post_types', function ( array $post_types ): array {
	unset( $post_types['product'] ); // WooCommerce already links the Shop page.
	return $post_types;
} );

The label comes from the post type’s archives label. To change the default, set 'labels' => array( 'archives' => 'All projects' ) when you register the post type. You can also rename each item after adding it, like any other menu item.

Gotchas

  • No archive, no item. If a post type is missing from the box, check that it was registered with 'has_archive' => true (or a slug string). After changing that, re-save Settings > Permalinks so the archive URL resolves.
  • Core has a hidden version. For post types with show_in_nav_menus enabled, WordPress adds an “All …” archive item at the top of the post type’s View All tab. Few people find it, and it disappears when show_in_nav_menus is false. The box above works either way.
  • Block themes. Appearance > Menus only exists when the theme supports classic menus. In a block theme’s Navigation block, add the archive as a link, or use the classic menu import.
  • Related: remove the post type slug from URLs changes what the archive URL looks like. To put the post type under categories as well, see Add Categories to a Custom Post Type in WordPress.

Snippet poll. Should this snippet be in the Scripts Organizer library for one-click import? 10 votes put it there.

0 of 10 votes

// newsletter

Three tested WordPress snippets a week

Blocks, security, performance, Tailwind, PHP and React. Copy, paste, ship. You’ll also get DPlugins product updates. No spam, and you can unsubscribe anytime.

// newsletter

Three tested WordPress snippets a week

Blocks, security, performance, Tailwind, PHP and React. Copy, paste, ship. You'll also get DPlugins product updates. No spam, and you can unsubscribe anytime.