I've developed a custom theme with a custom post type named events
. For some reason, however, WP refuses to use my archive page template with the file name archive-events.php
per WP's template hierarchy. WP keeps defaulting to index.php
as the template for this post type.
Previously I had a page configured in WP that was set to the slug /events/
which is now the slug of the custom post type. That page is now deleted, and I don't know if this is the issue that is causing WP to refuse to use archive-events.php
for my archive listing for the custom post type. I've tried modifying and re-saving my permalink structure, and that hasn't worked. Currently the permastruct is set to "Post name," i.e. /post-name/
More details:
events
(code below)
/events/
but is using index.php
even though I've created an archive template for the post type as archive-events.php
$GLOBALS['current_theme_template']
being used to render the page. This confirms that WP is using index.php
to render the archive page.header.php
file I'm echoing the function get_post_type_archive_link('events')
to confirm that WP thinks that the archive page for my custom post type should be /
archive.php
and index.php
WP templates are served as expected, but the template for the custom post type is skipped by WP no matter whatcpt-archive.php
as the template for the custom post type and serves either archive.php
or index.php
instead.Here's my functions.php
code to register the post type:
function registerEvents()
{
$labels = array(
'name' => _x( 'Events', 'Post type general name', 'textdomain' ),
'singular_name' => _x( 'Event', 'Post type singular name', 'textdomain' ),
'menu_name' => _x( 'Events', 'Admin Menu text', 'textdomain' ),
'name_admin_bar' => _x( 'Events', 'Add New on Toolbar', 'textdomain' ),
<snip>
);
$args = array(
'labels' => $labels,
'public' => true,
'hierarchical' => false,
'publicly_queryable' => true,
'exclude_from_search' => false,
'show_ui' => true,
'show_in_menu' => true,
'show_in_rest' => false,
'menu_position' => 5,
'menu_icon' => 'dashicons-calendar-alt',
'capability_type' => 'post',
'has_archive' => true,
'query_var' => true,
'delete_with_user' => false,
'supports' => array(
'title',
'editor',
'excerpt',
'thumbnail',
'page-attributes'
),
'taxonomies' => array( 'kind' ), // Custom tax previously registered
'rewrite' => array( 'slug' => 'events', 'with_front' => false ),
);
register_post_type( 'events', $args );
}
add_action( 'init', 'registerEvents' );
I've developed a custom theme with a custom post type named events
. For some reason, however, WP refuses to use my archive page template with the file name archive-events.php
per WP's template hierarchy. WP keeps defaulting to index.php
as the template for this post type.
Previously I had a page configured in WP that was set to the slug /events/
which is now the slug of the custom post type. That page is now deleted, and I don't know if this is the issue that is causing WP to refuse to use archive-events.php
for my archive listing for the custom post type. I've tried modifying and re-saving my permalink structure, and that hasn't worked. Currently the permastruct is set to "Post name," i.e. http://my.domain/post-name/
More details:
events
(code below)http://domain.com/events/post-name
/events/
but is using index.php
even though I've created an archive template for the post type as archive-events.php
$GLOBALS['current_theme_template']
being used to render the page. This confirms that WP is using index.php
to render the archive page.header.php
file I'm echoing the function get_post_type_archive_link('events')
to confirm that WP thinks that the archive page for my custom post type should be http://domain.com/events/
archive.php
and index.php
WP templates are served as expected, but the template for the custom post type is skipped by WP no matter whatcpt-archive.php
as the template for the custom post type and serves either archive.php
or index.php
instead.Here's my functions.php
code to register the post type:
function registerEvents()
{
$labels = array(
'name' => _x( 'Events', 'Post type general name', 'textdomain' ),
'singular_name' => _x( 'Event', 'Post type singular name', 'textdomain' ),
'menu_name' => _x( 'Events', 'Admin Menu text', 'textdomain' ),
'name_admin_bar' => _x( 'Events', 'Add New on Toolbar', 'textdomain' ),
<snip>
);
$args = array(
'labels' => $labels,
'public' => true,
'hierarchical' => false,
'publicly_queryable' => true,
'exclude_from_search' => false,
'show_ui' => true,
'show_in_menu' => true,
'show_in_rest' => false,
'menu_position' => 5,
'menu_icon' => 'dashicons-calendar-alt',
'capability_type' => 'post',
'has_archive' => true,
'query_var' => true,
'delete_with_user' => false,
'supports' => array(
'title',
'editor',
'excerpt',
'thumbnail',
'page-attributes'
),
'taxonomies' => array( 'kind' ), // Custom tax previously registered
'rewrite' => array( 'slug' => 'events', 'with_front' => false ),
);
register_post_type( 'events', $args );
}
add_action( 'init', 'registerEvents' );
The solution to this problem turned out to be:
/events/
) to "event" (with the slug /event/
)/events/
to display the archives of the custom post typeWhile this is technically not an answer to the question of why WP refuses to serve and display the archive template for the custom post typer per WP's own template hierarchy, it solves my problem.
Along the way, I used wp-cli to flush WP's internal cache and delete all transients, but that didn't have any effect.
If you're registering post types manually (as opposed to using CPT UI or something similar) then you need to remember to go into settings > permalinks and just click 'save'. That's caught me out a couple of times.