0

My question is similar to a number of posts on this site, but I haven't found a solution to my specific issue yet.

I'm trying to implement an archive for a custom post type, "ttwi_course". I completed the following steps, which I've successfully performed on numerous other sites:

  1. I registered the CPT, code below
  2. I created an archive template, archive-ttwi_course.php
  3. I created a single-ttwi_course.php
  4. I created a WordPress Page called "Courses"

When I visit the Courses Page (http://localsite.dev/courses/) the page exists but uses the page.php template instead of archive-ttwi_course.php. When I visit a single ttwi_course post (http://localsite.dev/courses/course-1), it uses the correct single-ttwi_course.php template. I tried deleting the Courses WP Page, but http://localsite.dev/courses/ results in a 404.

Note that I've already tried flushing permalinks, adding and removing flush_rewrite_rules(); after register_post_type( 'ttwi_course', $args );, changing themes, disabling plugins, and removing all code from the archive template. It's probably some dumb issue, but I have yet to find it.

Registered post type:

add_action( 'init', 'ttw_register_course_post_type' );    
function ttw_register_course_post_type() {
$labels = array(
    'name'               => __( 'Courses', 'ttwi-courses' ),
    'singular_name'      => __( 'Course', 'ttwi-courses' ),
    'menu_name'          => __( 'Courses', 'ttwi-courses' ),
    'name_admin_bar'     => __( 'Course', 'add new on admin bar', 'ttwi-courses' ),
    'add_new'            => __( 'Add New', 'ttwi-courses' ),
    'add_new_item'       => __( 'Add New Course', 'ttwi-courses' ),
    'new_item'           => __( 'New Course', 'ttwi-courses' ),
    'edit_item'          => __( 'Edit Course', 'ttwi-courses' ),
    'view_item'          => __( 'View Course', 'ttwi-courses' ),
    'all_items'          => __( 'All Courses', 'ttwi-courses' ),
    'search_items'       => __( 'Search Courses', 'ttwi-courses' ),
    'parent_item_colon'  => __( 'Parent Course:', 'ttwi-courses' ),
    'not_found'          => __( 'No courses found.', 'ttwi-courses' ),
    'not_found_in_trash' => __( 'No courses found in Trash.', 'ttwi-courses' )
);

$args = array(
    'labels'             => $labels,
    'public'             => true,
    'publicly_queryable' => true,
    'show_ui'            => true,
    'show_in_menu'       => true,
    'query_var'          => true,
    'rewrite'            => array( 'slug' => 'courses' ),
    'has_archive'        => true,
    'hierarchical'       => false,
    'menu_position'      => 25,
    'supports'           => array( 'title', 'editor', 'thumbnail', 'excerpt', 'page-attributes' )
);

register_post_type( 'ttwi_course', $args );
}
1

Everything you describe is expected behavior based on your post type registration settings. A page isn't going to load an archive template, I'm not sure why you'd expect it to. Your rewrite argument slug is only for single posts. Setting has_archive to true uses the post type slug as archive location, which is ttwi_course. If you want the archive to be courses, then set has_archive to courses.

  • Thanks Milo. You're right, setting has_archive to 'courses' works. I wonder why creating a Page previously had worked before? – scpers0n Nov 6 '15 at 21:52

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.