0

I am getting a 404 on my single custom post types whilst using the 'rewrite' option in the custom taxonomy. If I take the rewrite out the single page works but the custom taxonomy archive then 404's instead.

Here is the code for my custom post type;

/* Training Post Type */
function add_training_post_type() {
  $labels = array(
    'name' => _x('Training', 'post type general name'),
    'singular_name' => _x('Training material', 'post type singular name'),
    'add_new' => _x('Add Training','Training'),
    'add_new_item' => __('Add new Training'),
    'edit_item' => __('Edit Training'),
    'new_item' => __('New Training'),
    'all_items' => __('All Training'),
    'view_item' => __('View Training'),
    'search_items' => __('Search Training'),
    'not_found' =>  __('No Training found'),
    'not_found_in_trash' => __('No Training in Trash'), 
    'parent_item_colon' => '',
    'menu_name' => __('Training')
  );

  $args = array(
   'labels' => $labels,
    'public' => true,
    'publicly_queryable' => true,
    'show_ui' => true, 
    'show_in_menu' => true, 
    'query_var' => true,
    'rewrite' => true,
    'capability_type' => 'post',
    'has_archive' => true, 
    'hierarchical' => false,
    'menu_position' => null,
    'supports' => array('title')
  ); 
  register_post_type('training-and-events',$args);
  flush_rewrite_rules();
}
add_action( 'init', 'add_training_post_type' );

And here's my code for the custom taxonomy;

/* Training Tax */
add_action( 'init', 'create_training_taxonomies', 0 );

function create_training_taxonomies() 
{
  $labels = array(
    'name' => _x( 'Training categories', 'taxonomy general name' ),
    'singular_name' => _x( 'Training category', 'taxonomy singular name' ),
    'search_items' =>  __( 'Search Training categories' ),
    'popular_items' => __( 'Popular Training categories' ),
    'all_items' => __( 'All Training categories' ),
    'parent_item' => null,
    'parent_item_colon' => null,
    'edit_item' => __( 'Edit Training category' ), 
    'update_item' => __( 'Update Training category' ),
    'add_new_item' => __( 'Add Training category' ),
    'new_item_name' => __( 'New Training category' ),
    'menu_name' => __( 'Training categories' )
  ); 

   register_taxonomy('training-category',array('training-and-events'), array(
    'hierarchical' => true,
    'labels' => $labels,
    'show_ui' => true,
    'query_var' => true,
    'rewrite' => array('slug' => 'training-and-events')  
  ));
  flush_rewrite_rules();
}

(The flush_rewrite_rules(); is just while I'm developing, I will remove once I have it working)

Does anyone know when I'm doing incorrectly here, I think I've got blindness as now that I have been staring at the code so long and it's feeling like spaghetti in my head ):

Thanks, Harry.

1 Answer 1

0

Add priority with at least 1 to your add actions:

add_action( 'init', 'add_training_post_type', 1 ); add_action( 'init', 'create_training_taxonomies', 1 );

Currently your issue is that your post type and taxonomy rewrite slugs are identical, so the URL base would be the same and it confuses WP. Fortunately there is easy solution, just add this code to your functions.php file

function taxonomy_slug_rewrite($wp_rewrite) {
    $rules = array();
    // get all custom taxonomies
    $taxonomies = get_taxonomies(array('_builtin' => false), 'objects');
    // get all custom post types
    $post_types = get_post_types(array('public' => true, '_builtin' => false), 'objects');

    foreach ($post_types as $post_type) {
        foreach ($taxonomies as $taxonomy) {

            // go through all post types which this taxonomy is assigned to
            foreach ($taxonomy->object_type as $object_type) {

                // check if taxonomy is registered for this custom type
                if ($object_type == $post_type->rewrite['slug']) {

                    // get category objects
                    $terms = get_categories(array('type' => $object_type, 'taxonomy' => $taxonomy->name, 'hide_empty' => 0));

                    // make rules
                    foreach ($terms as $term) {
                        $rules[$object_type . '/' . $term->slug . '/?$'] = 'index.php?' . $term->taxonomy . '=' . $term->slug;
                    }
                }
            }
        }
    }
    // merge with global rules
    $wp_rewrite->rules = $rules + $wp_rewrite->rules;
}
add_filter('generate_rewrite_rules', 'taxonomy_slug_rewrite');

And to be safe i would go to your permalinks page and just press save button to be sure.

1
  • Hi Martinn, thanks for your reply. Your code worked perfectly after I set the rewrite on my custom taxonomy to false - you saved my head a lot of pain :) Thanks, Harry. Mar 31, 2016 at 11:09

Your Answer

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

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