1

I'm working with custom post types in WordPress and have a question about slugs.

Let's say that we need to create custom post type Quiz with taxonomy Quiz Category. I've used the following code:

// Create and register custom post type quiz
function create_posttype_quiz() {
    $labels = array(
        'name'               => _x( 'Quiz', 'post type general name' ),
        'singular_name'      => _x( 'Quiz', 'post type singular name' ),
        'add_new'            => _x( 'Add New', 'quiz' ),
        'add_new_item'       => __( 'Add New Quiz' ),
        'edit_item'          => __( 'Edit Quiz' ),
        'new_item'           => __( 'New Quiz' ),
        'all_items'          => __( 'All Quiz' ),
        'view_item'          => __( 'View Quiz' ),
        'search_items'       => __( 'Search Quiz' ),
        'not_found'          => __( 'No quiz found' ),
        'not_found_in_trash' => __( 'No quiz found in the Trash' ), 
        'parent_item_colon'  => '',
        'menu_name'          => 'Quizzes'
    );
    $args = array(
        'labels'        => $labels,
        'description'   => 'Holds our quiz and quiz specific data',
        'public'        => true,
        'menu_position' => 5,
        'supports'      => array( 'title', 'editor' ),
        'has_archive'   => true,
        'rewrite' => array('slug' => 'quiz'),
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_nav_menus'   => true,

    );

    register_post_type( 'quiz', $args );    
}
add_action( 'init', 'create_posttype_quiz' );

// Create and register quiz categories
function create_taxonomies_quiz() {
    $labels = array(
        'name'              => _x( 'Quiz Categories', 'taxonomy general name' ),
        'singular_name'     => _x( 'Quiz Category', 'taxonomy singular name' ),
        'search_items'      => __( 'Search Quiz Categories' ),
        'all_items'         => __( 'All Quiz Categories' ),
        'parent_item'       => __( 'Parent Quiz Category' ),
        'parent_item_colon' => __( 'Parent Quiz Category:' ),
        'edit_item'         => __( 'Edit Quiz Category' ), 
        'update_item'       => __( 'Update Quiz Category' ),
        'add_new_item'      => __( 'Add New Quiz Category' ),
        'new_item_name'     => __( 'New Quiz Category' ),
        'menu_name'         => __( 'Quiz Categories' ),
    );
    $args = array(
        'labels' => $labels,
        'hierarchical' => true,
        //'show_admin_column'=> true,
        'rewrite' => array('hierarchical'=>true, 'slug'=>'quizzes', 'with_front'=>false),
    );
    register_taxonomy( 'quiz_category', 'quiz', $args );
}
add_action( 'init', 'create_taxonomies_quiz', 0 );

Everything works fine, I'm able to create and edit quizzes, add them to quiz categories etc. Also, I can access quizzes on frontend, e.g. site.com/quiz/sample-quiz-one, site.com/quiz/sample-quiz-two... and list quizzes in quiz categories, e.g. site.com/quizzes/category-one, site.com/quizzes/category-two...

The problem is that I can't list quizzes under site.com/quizzes but at site.com/quiz instead. (site.com/quizzes will create page not found error).

Basically, I would like to be able to:

  • access a single quiz at url: site.com/quiz/sample-quiz-one
  • list all quizzes at site.com/quizzes rather than site.com/quiz.

Is it possible to achieve this without htaccess or any other redirection, but using the code above (maybe some args I'm not aware of)?

1 Answer 1

2

Your issue is that WordPress does not have archive for all posts in specific taxonomy, only archives for posts in each term of that taxonomy.

In your CPT registration you can set 'has_archive' => 'quizzes' so that post type archive slug is different from singular slug. However I am not sure if that will work properly when your taxonomy is also using that slug for term archives.

2
  • I've tested and it works +1.
    – gmazzap
    Commented Sep 10, 2013 at 14:12
  • I can confirm. The issue fixed. Commented Sep 10, 2013 at 14:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.