I have a custom post type and one custom taxonomy registered for it. The custom post type is called “memory-cards” and the taxonomy is “lesson”.For this taxonomy I have create two terms “english” and “french”.

When I go to this url:

example.com/lesson/english

or

example.com/lesson/french

Everything goes as expeted and it directs me to archive page or if I create taxonomy.php or taxonomy-english.php.

But when I try

example.com/lesson

I get page not found error.

My question is if I’m doing something wrong here? Aren’t I supposed to get to the archive page for all the “lesson”s?
I have tried these in my register taxonomy arguments:

        'rewrite'           => array( 'slug' => 'lesson' ),

and

        'rewrite'           => true,

but none worked.
I have seen other question similar to this but they are mostly concerned with the terms link rather than the taxonomy link. Also most tutorials discuss the hierarchy of taxonomy rather that the link of taxonomy.

This is the code for creating the taxonomy:

    $labels = array(
    'name'              => _x( 'Lessons', 'taxonomy general name', 'textdomain' ),
    'singular_name'     => _x( 'Lesson', 'taxonomy singular name', 'textdomain' ),
    'search_items'      => __( 'Search Lessons', 'textdomain' ),
    'all_items'         => __( 'All Lessons', 'textdomain' ),
    'parent_item'       => __( 'Parent Lesson', 'textdomain' ),
    'parent_item_colon' => __( 'Parent Lesson:', 'textdomain' ),
    'edit_item'         => __( 'Edit Lesson', 'textdomain' ),
    'update_item'       => __( 'Update Lesson', 'textdomain' ),
    'add_new_item'      => __( 'Add New Lesson', 'textdomain' ),
    'new_item_name'     => __( 'New Lesson Name', 'textdomain' ),
    'menu_name'         => __( 'Lessons', 'textdomain' ),
);

$args = array(
    'hierarchical'      => true,
    'labels'            => $labels,
    'show_ui'           => true,
    'show_admin_column' => true,
    'query_var'         => true,
    'rewrite'           => array( 'slug' => 'lesson'),
);

register_taxonomy( 'lesson', array( 'memory-cards' ), $args );  

1 Answer
1

But when I try

example.com/lesson

I get page not found error.

Like I pointed in my comment to your question, that’s actually how it works, where the term slug needs to be specified in the URL. That’s why example.com/lesson/english (term slug is english) and example.com/lesson/french (term slug is french) work, but not example.com/lesson (term slug not specified).

So one way to make example.com/lesson works, is by setting the has_archive parameter to lesson when registering the CPT memory-cards via register_post_type(). For example:

register_post_type( 'memory-cards', array(
    'labels'       => array(
        'name'          => 'Memory Cards',
        'singular_name' => 'Memory Card',
    ),
    'public'       => true,
    'has_archive'  => 'lesson',
) );

But that also means, example.com/memory-cards, which is the default archive URL for your memory-cards CPT (when has_archive is set to true), would no longer display the CPT archive.

To fix that, or to preserve the default archive URL, you can use add_rewrite_rule() to add the rewrite rules for example.com/lesson, like so:

register_taxonomy( 'lesson', 'memory-cards', array(
    'labels'       => array(
        'name'          => 'Lessons',
        'singular_name' => 'Lesson',
    ),
    'rewrite'      => true,
    // ...other args here...
) );

add_rewrite_rule( 'lesson/?$', 'index.php?post_type=memory-cards', 'top' );
add_rewrite_rule( 'lesson/page/(\d+)/?$', 'index.php?post_type=memory-cards&paged=$matches[1]', 'top' );

The second rewrite rule will handle paged requests such as example.com/lesson/page/2/.

Leave a Reply

Your email address will not be published. Required fields are marked *