WordPress.org

Ready to get started?Download WordPress

Forums

rewrites not working with CPT and custom Taxonomy (2 posts)

  1. rikdeek
    Member
    Posted 4 months ago #

    Hello,

    – What I'm trying to do –
    I’m trying to extend an existing WordPress site, to add a directory of courses that are running at the college I work at.

    – What I’ve tried –
    I’ve created a custom post type called “Courses” and I’ve linked a custom taxonomy to it called “Subjects” (hierarchical like categories).
    Below is what I have in my functions.php:

    <?php
    
    /* -- Register Custom Post Type -- */
    
    if ( ! function_exists('sed_courses_cpt') ) {
    
    // Register Custom Post Type
    function sed_courses_cpt() {
    
    	$labels = array(
    		'name'                => 'Courses',
    		'singular_name'       => 'Course',
    		'menu_name'           => 'Course',
    		'parent_item_colon'   => 'Parent Course:',
    		'all_items'           => 'All Courses',
    		'view_item'           => 'View Course',
    		'add_new_item'        => 'Add New Course',
    		'add_new'             => 'New Course',
    		'edit_item'           => 'Edit Course',
    		'update_item'         => 'Update Course',
    		'search_items'        => 'Search courses',
    		'not_found'           => 'No courses found',
    		'not_found_in_trash'  => 'No courses found in Trash',
    	);
    	$rewrite = array(
    		'slug'                => 'courses',
    		'with_front'          => true,
    		'pages'               => true,
    		'feeds'               => true,
    	);
    	$args = array(
    		'label'               => 'courses',
    		'description'         => 'Courses at Swarthmore',
    		'labels'              => $labels,
    		'supports'            => array( 'title', 'editor', 'thumbnail', 'custom-fields', 'page-attributes', 'excerpt', ),
    		'taxonomies'          => array( 'courses' ),
    		'hierarchical'        => true,
    		'public'              => true,
    		'show_ui'             => true,
    		'show_in_menu'        => true,
    		'show_in_nav_menus'   => true,
    		'show_in_admin_bar'   => true,
    		'menu_position'       => 5,
    		'menu_icon'           => '',
    		'can_export'          => true,
    		'has_archive'         => true,
    		'exclude_from_search' => false,
    		'publicly_queryable'  => true,
    		'query_var'           => 'course',
    		'rewrite'             => $rewrite,
    		'capability_type'     => 'page',
    	);
    	register_post_type( 'courses', $args );
    
    }
    
    // Hook into the 'init' action
    add_action( 'init', 'sed_courses_cpt', 0 );
    
    }
    
    /* -- Register Custom Taxonomy -- */
    
    if ( ! function_exists('sec_subjects_ctx') ) {
    
    // Register Custom Taxonomy
    function sec_subjects_ctx()  {
    
    	$labels = array(
    		'name'                       => 'Subjects',
    		'singular_name'              => 'Subject',
    		'menu_name'                  => 'Subject',
    		'all_items'                  => 'All Subjects',
    		'parent_item'                => 'Parent Subject',
    		'parent_item_colon'          => 'Parent Subject:',
    		'new_item_name'              => 'New Subject Name',
    		'add_new_item'               => 'Add New Subject',
    		'edit_item'                  => 'Edit Subject',
    		'update_item'                => 'Update Subject',
    		'separate_items_with_commas' => 'Separate subjects with commas',
    		'search_items'               => 'Search subjects',
    		'add_or_remove_items'        => 'Add or remove subjects',
    		'choose_from_most_used'      => 'Choose from the most used subjects',
    	);
    	$rewrite = array(
    		'slug'                       => 'subjects',
    		'with_front'                 => true,
    		'hierarchical'               => true,
    	);
    	$args = array(
    		'labels'                     => $labels,
    		'hierarchical'               => true,
    		'public'                     => true,
    		'show_ui'                    => true,
    		'show_admin_column'          => true,
    		'show_in_nav_menus'          => true,
    		'show_tagcloud'              => true,
    		'query_var'                  => 'subjects',
    		'rewrite'                    => $rewrite,
    	);
    	register_taxonomy( 'subjects', 'courses', $args );
    
    }
    
    // Hook into the 'init' action
    add_action( 'init', 'sec_subjects_ctx', 0 );
    
    }
    
    /* Flush rewrite rules for custom post type and taxonomy. */
    add_action( 'after_switch_theme', 'sec_flush_rewrite_rules' );
    
    /* Flush your rewrite rules */
    function sec_flush_rewrite_rules() {
         flush_rewrite_rules();
    }
    
    ?>

    I’ve got the data from our MSSQL database in to WordPress's tables and the courses and subjects all appear properly in their own place in the admin UI under a new menu called Courses. So this surely means that the the custom post type and taxonomy are being created properly. Extra data is stored in meta tags.

    I would like the urls to work as follows:

    domain.com/courses/ - Should list all courses (CPT)
    domain.com/courses/course-title - Should list a single course

    domain.com/subjects/ - Should list all subjects (taxonomy)
    domain.com/subjects/subject-title - Should list all courses in that subject

    Everywhere I have read about custom post types it mentions that WordPress should create rewrites once the CPT is registered, although I can't get this to work.

    I've tried to create templates for the custom post type and taxonomy, but I've realised that I'm jumping the gun here as I need to get rewrites working properly first. I've also added the flush_rewrite_rules() function to functions.php in my theme.

    – The problem –

    If I set the rewrites in Settings > Permalinks to "Default" style, then if I go to to Courses or Subjects in the admin interface and hover over a course or subject and click "view", I am taken to a single view of a course or subject respectively. Although visiting domain.com/courses/ or domain.com/subjects/ shows a 404 error.

    If I set the permalinks to "Pretty" (/%postname%/ or /%category%/%postname%/) in Settings > Permalinks, then clicking on a "view" link as above results in 404s.

    Looking at the output from the "Rewrite Rules Inspector" plugin with permalinks set back to /%postname%/, there are listings which show my CPT and taxonomy in the "Source" column.

    I thought that the permalinks for the custom post type and taxonomy were controlled through the things in functions.php and were separate to the permalinks settings for the site in general (in Settings > Permalinks). Is this not the case?

    Am I missing something?

    Any help or pointers would be greatly appreciated.
    Kind regards,
    - Rik

  2. rikdeek
    Member
    Posted 4 months ago #

    I've now tried a fresh installation of WordPress 3.8.2.
    Without importing my data, I added the above code to my functions.php file. The custom post type and taxonomy are created in the UI, so I added some example courses and linked them to subjects. I set my .htaccess to 777.

    Again, default permalinks show the page with $_GET style links, but if I change the permalinks to pretty, I get a "NOT FOUND" page, for all of the following urls:

    http://localhost/fresh/courses/counselling/
    http://localhost/fresh/courses/
    http://localhost/fresh/course/

    I've even tried changing the settings in the $rewrite arrays for the CPT and taxonomy, setting 'with_front' => false,
    I still cannot get permalinks working.

    I'm not sure what to try next.

    I hope that I am missing something obvious and that you can help me.

    All the best,
    - Rik

Reply

You must log in to post.

About this Topic