WordPress custom post type archive-<post type>.php not working

New to WordPress, I’ve been trying to create an archive page for a custom post type, and when I click the link to localhost/websitename/wordpress/archive-event.php I get a 404. Here is my code to register the post type:

add_action('init', 'event_register');

function event_register() {

    $labels = array(
        'name' => _x('Events', 'post type general name'),
        'singular_name' => _x('Event', 'post type singular name'),
        'add_new' => _x('Add New', 'event'),
        'add_new_item' => __('Add New Event'),
        'edit_item' => __('Edit Event'),
        'new_item' => __('New Event'),
        'view_item' => __('View Event'),
        'search_items' => __('Search Events'),
        'not_found' =>  __('Nothing found'),
        'not_found_in_trash' => __('Nothing found in Trash'),
        'parent_item_colon' => ''
    );

    $args = array(
        'labels' => $labels,
        'public' => true,
        'publicly_queryable' => true,
        'show_ui' => true,
        'query_var' => true,
        'rewrite' => array('slug' => 'event'),
        'has_archive' => 'event',
        'capability_type' => 'post',
        'hierarchical' => false,
        'menu_position' => null,
        'supports' => array('title','editor','thumbnail'),
    ); 

    register_post_type( 'event' , $args );

}

I’ve tried having rewrite => true, has_archive => true, flushing the rewrite rules, and even registering a taxonomy for the post type. single-event.php works fine. What now?

Related posts

Leave a Reply

5 comments

  1. Only two things required for custom post type archive page.

    1) has_archive should be true

    2) You need to flush the permalink cache once after code update.

    functions.php

    function my_custom_posts() {
    
        $labels = array(
    
            'name'               => _x( 'Events', 'post type general name' ),
            'singular_name'      => _x( 'Event', 'post type singular name' ),
            'add_new'            => _x( 'Add New', 'event' ),
            'add_new_item'       => __( 'Add New Event' ),
            'edit_item'          => __( 'Edit Event' ),
            'new_item'           => __( 'New Event' ),
            'all_items'          => __( 'All Events' ),
            'view_item'          => __( 'View Event' ),
            'search_items'       => __( 'Search Events' ),
            'not_found'          => __( 'No events found' ),
            'not_found_in_trash' => __( 'No events found in the Trash' ), 
            'parent_item_colon'  => '',
            'menu_name'          => 'Events'
    
        );
    
        $args = array(
    
            'labels'        => $labels,
            'description'   => 'Holds our events and event specific data',
            'public'        => true,
            'menu_position' => 5,
            'supports'      => array( 'title' ),
            'has_archive'   => true, // only this is required to enable archive page else 404 error will occur
            'publicly_queryable' => true,
            'show_ui' => true,
            'query_var' => true,
            'rewrite' => array('slug' => 'events', 'with_front' => true),
            'capability_type' => 'post',
            'hierarchical' => false,
    
        );
    
        register_post_type( 'event', $args );   
        //flush_rewrite_rules();
    
    }
    
    add_action( 'init', 'my_custom_posts' );
    

    Default archive.php will work but if you wish to overwrite default one, you have to use a proper archive template archive-<custom_post_slug>.php, e.g.

    archive-events.php
    

    Also, if you’re just registering your post type, you will need to flush the permalink cache. Do this by changing permalink structure in to WordPress admin.

    Now you can access archive page https://domain/post_slug/

    Note: if you choose Numeric in permalink structure url would be https://domain/archives/post_slug/

  2. not sure this will answer your question but did you reset your permalink structure?

    If you just added a custom post type, you will need go to Permalinks page and press Save.

    Hope that helps!

  3. It looks like in your URL example you’re attempting to get to the actual template file name itself.

    But if you’ve defined the slug to be event you should be able to simply visit localhost/websitename/wordpress/event

  4. I was using CPT UI plugin where “Has Archive” flag was set to False by default.
    You can change this in:

    "Edit Post Type" tab > "Settings" > "Has Archive"
    

    set it to True, instead and don’t forget to flush Permalinks (click Save on Permalinks page).

  5. There is issue in Permalink structure. I have change settings => my Permalink to “Day and name” or “Month and name” or “Post name” which has fixed my issue.