I'm getting quite frustrated setting up the correct URL structure for a Cutom Post Type with a Custom taxonomy.
I have a Custom Post Type called courses
with a Custom Taxonomy called course-type
.
- The URL structure should be:
site.com/courses/course-type/course-single-post/
. - For example:
site.com/courses/science/rocket-to-the-moon/
I've managed to achieve that however not all URL parts are behaving as it should.
site.com/courses/
- returns a 404site.com/courses/science/
- Shows an archive page with all posts in side that custom taxonomy which is correctsite.com/courses/science/rocket-to-the-moon/
- Shows the single Custom Post Type which is correct too
I don't know why the site.com/courses/
returns a 404 instead of showing an archive page listing all Custom Post Type's...?
This is the code that I've used:
<?php
/*Courses Custom Post Type*/
function my_custom_post_courses() {
$labels = array(
'name' => _x( 'Courses', 'post type general name' ),
'singular_name' => _x( 'Course', 'post type singular name' ),
'add_new' => _x( 'New course', 'reis' ),
'add_new_item' => __( 'Add new course' ),
'edit_item' => __( 'Edit course' ),
'new_item' => __( 'New item' ),
'all_items' => __( 'All courses' ),
'view_item' => __( 'View courses' ),
'search_items' => __( 'Search courses' ),
'not_found' => __( 'Nothing found ' ),
'not_found_in_trash' => __( 'Nothing found in the trash' ),
'parent_item_colon' => '',
'menu_name' => 'Courses'
);
$args = array(
'labels' => $labels,
'description' => 'Enter a new course',
'public' => true,
'menu_position' => 5,
'supports' => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments' ),
'has_archive' => true,
'hierarchical' => true,
'rewrite' => array('slug' => 'courses/%course-type%','with_front' => false),
'query_var' => true,
//'rewrite' => true,
//'publicly_queryable' => false,
);
register_post_type( 'courses', $args );
}
add_action( 'init', 'my_custom_post_courses' );
/* Courses custom taxonomy */
function my_taxonomies_course_type() {
$labels = array(
'name' => _x( 'Course type', 'taxonomy general name' ),
'singular_name' => _x( 'Course type', 'taxonomy singular name' ),
'search_items' => __( 'Search course types' ),
'all_items' => __( 'All course types' ),
'parent_item' => __( 'Parent course type' ),
'parent_item_colon' => __( 'Parent course type:' ),
'edit_item' => __( 'Edit course type' ),
'update_item' => __( 'Update course type ' ),
'add_new_item' => __( 'Add new course type' ),
'new_item_name' => __( 'New course type' ),
'menu_name' => __( 'Course type' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
'public' => true,
'query_var' => 'course-type',
'rewrite' => array('slug' => 'courses' ),
'_builtin' => false,
);
register_taxonomy( 'course-type', 'courses', $args );
}
add_action( 'init', 'my_taxonomies_course_type', 0 );
/* Permalink filter Courses */
add_filter('post_link', 'course_permalink', 1, 3);
add_filter('post_type_link', 'course_permalink', 1, 3);
function course_permalink($permalink, $post_id, $leavename) {
if (strpos($permalink, '%course-type%') === FALSE) return $permalink;
// Get post
$post = get_post($post_id);
if (!$post) return $permalink;
// Get taxonomy terms
$terms = wp_get_object_terms($post->ID, 'course-type');
if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0]))
$taxonomy_slug = $terms[0]->slug;
else $taxonomy_slug = 'no-course-type';
return str_replace('%course-type%', $taxonomy_slug, $permalink);
}
Dashboard -> Settings -> Permalinks
.has_archive
rather than setting it totrue
.