Pagination for custom taxonomy 404
-
I have a custom post type case-study which I have a number of taxonomies registered. I was looking to create the following URL structure:
- Archive: domain.com/post_type/
- Taxonomy: domain.com/post_type/tax_name/
- Post: domain.com/post_type/post_name/
The reasoning behind this is that a single post can have multiple taxonomies.
The issue I have is that on the taxonomy page, when I list out the related posts, if I use the
posts_nav_link()
it works for the main archive page, but returns a 404 for each taxonomy, with the exception on one.Here is how I’ve registered the post type:
function viamo_register_post_types() { $case_studies_labels = array( 'name' => __( 'Case Studies', 'viamo' ), 'singular_name' => __( 'Case Study', 'viamo' ), 'add_new' => _x( 'Add New Case Study', 'viamo', 'viamo' ), 'add_new_item' => __( 'Add New Case Study', 'viamo' ), 'edit_item' => __( 'Edit Case Study', 'viamo' ), 'new_item' => __( 'New Case Study', 'viamo' ), 'view_item' => __( 'View Case Study', 'viamo' ), 'search_items' => __( 'Search Case Studies', 'viamo' ), 'not_found' => __( 'No Services found', 'viamo' ), 'not_found_in_trash' => __( 'No Services found in Trash', 'viamo' ), 'parent_item_colon' => __( 'Parent Service:', 'viamo' ), 'menu_name' => __( 'Case Studies', 'viamo' ), ); $case_studies_args = array( 'labels' => $case_studies_labels, 'hierarchical' => false, 'public' => true, 'show_ui' => true, 'show_in_menu' => true, 'show_in_admin_bar' => true, 'menu_position' => null, 'menu_icon' => 'dashicons-awards', 'show_in_nav_menus' => true, 'publicly_queryable' => true, 'exclude_from_search' => false, 'has_archive' => 'case-studies', 'query_var' => true, 'can_export' => true, 'rewrite' => array( 'slug' => 'case-studies' ), 'capability_type' => 'post', 'supports' => array('title', 'thumbnail', 'editor', 'revisions') ); register_post_type( 'case-study', $case_studies_args ); } add_action( 'init', 'viamo_register_post_types' );
The taxonomies are then registered as follows:
function viamo_register_taxonomomies() { $sector_labels = array( 'name' => _x( 'Sectors', 'Taxonomy plural name', 'viamo' ), 'singular_name' => _x( 'Sector', 'Taxonomy singular name', 'viamo' ), 'search_items' => __( 'Search Sectors', 'viamo' ), 'popular_items' => __( 'Popular Sectors', 'viamo' ), 'all_items' => __( 'All Sectors', 'viamo' ), 'parent_item' => __( 'Parent Sector', 'viamo' ), 'parent_item_colon' => __( 'Parent Sector', 'viamo' ), 'edit_item' => __( 'Edit Sector', 'viamo' ), 'update_item' => __( 'Update Sector', 'viamo' ), 'add_new_item' => __( 'Add New Sector', 'viamo' ), 'new_item_name' => __( 'New Sector Name', 'viamo' ), 'add_or_remove_items' => __( 'Add or remove Sectors', 'viamo' ), 'choose_from_most_used' => __( 'Choose from most used Sector', 'viamo' ), 'menu_name' => __( 'Sectors', 'viamo' ), ); $sector_args = array( 'labels' => $sector_labels, 'public' => true, 'show_in_nav_menus' => true, 'show_admin_column' => false, 'hierarchical' => true, 'show_tagcloud' => true, 'show_ui' => true, 'query_var' => true, 'rewrite' => array( 'slug' => 'case-studies'), 'query_var' => true, 'capabilities' => array(), ); register_taxonomy( 'sector', array( 'case-study'), $sector_args ); $locations_labels = array( 'name' => _x( 'Locations', 'Taxonomy plural name', 'viamo' ), 'singular_name' => _x( 'Location', 'Taxonomy singular name', 'viamo' ), 'search_items' => __( 'Search Locations', 'viamo' ), 'popular_items' => __( 'Popular Locations', 'viamo' ), 'all_items' => __( 'All Locations', 'viamo' ), 'parent_item' => __( 'Parent Location', 'viamo' ), 'parent_item_colon' => __( 'Parent Location', 'viamo' ), 'edit_item' => __( 'Edit Location', 'viamo' ), 'update_item' => __( 'Update Location', 'viamo' ), 'add_new_item' => __( 'Add New Location', 'viamo' ), 'new_item_name' => __( 'New Location Name', 'viamo' ), 'add_or_remove_items' => __( 'Add or remove Locations', 'viamo' ), 'choose_from_most_used' => __( 'Choose from most used Location', 'viamo' ), 'menu_name' => __( 'Locations', 'viamo' ), ); $locations_args = array( 'labels' => $locations_labels, 'public' => true, 'show_in_nav_menus' => true, 'show_admin_column' => false, 'hierarchical' => true, 'show_tagcloud' => true, 'show_ui' => true, 'query_var' => true, 'rewrite' => array( 'slug' => 'case-studies'), 'query_var' => true, 'capabilities' => array(), ); register_taxonomy( 'location', array( 'case-study'), $locations_args ); $service_labels = array( 'name' => _x( 'Services', 'Taxonomy plural name', 'viamo' ), 'singular_name' => _x( 'Service', 'Taxonomy singular name', 'viamo' ), 'search_items' => __( 'Search Services', 'viamo' ), 'popular_items' => __( 'Popular Services', 'viamo' ), 'all_items' => __( 'All Services', 'viamo' ), 'parent_item' => __( 'Parent Service', 'viamo' ), 'parent_item_colon' => __( 'Parent Service', 'viamo' ), 'edit_item' => __( 'Edit Service', 'viamo' ), 'update_item' => __( 'Update Service', 'viamo' ), 'add_new_item' => __( 'Add New Service', 'viamo' ), 'new_item_name' => __( 'New Service Name', 'viamo' ), 'add_or_remove_items' => __( 'Add or remove Services', 'viamo' ), 'choose_from_most_used' => __( 'Choose from most used Service', 'viamo' ), 'menu_name' => __( 'Services', 'viamo' ), ); $service_args = array( 'labels' => $service_labels, 'public' => true, 'show_in_nav_menus' => true, 'show_admin_column' => false, 'hierarchical' => true, 'show_tagcloud' => true, 'show_ui' => true, 'query_var' => true, 'rewrite' => array( 'slug' => 'case-studies'), 'query_var' => true, 'capabilities' => array(), ); register_taxonomy( 'service', array( 'case-study' ), $service_args ); } add_action( 'init', 'viamo_register_taxonomomies' );
Following an article I read to achieve my desired URL’s I then use the following function to extend the generate_rewrite_rules hook:
function generate_taxonomy_rewrite_rules( $wp_rewrite ) { $rules = array(); $post_types = get_post_types(array('name' => 'case-study', 'public' => true, '_builtin' => false ), 'objects'); $taxonomies = array( 'sector' => get_taxonomies(array('name' => 'sector', 'public' => true, '_builtin' => false ), 'objects'), 'service' => get_taxonomies(array('name' => 'service', 'public' => true, '_builtin' => false ), 'objects'), 'location' => get_taxonomies(array('name' => 'location', 'public' => true, '_builtin' => false ), 'objects') ); foreach ( $post_types as $post_type ) { $post_type_name = $post_type->name; // 'case-study' $post_type_slug = $post_type->rewrite['slug']; // 'case-studies' foreach ( $taxonomies as $key => $taxonomy ) { $taxonomy = $taxonomy[$key]; if ( $taxonomy->object_type[0] == $post_type_name ) { $terms = get_categories( array( 'type' => $post_type_name, 'taxonomy' => $taxonomy->name, 'hide_empty' => 0 ) ); foreach ( $terms as $term ) { $rules[$post_type_slug . '/' . $term->slug . '/?$'] = 'index.php?' . $term->taxonomy . '=' . $term->slug; } } } } $wp_rewrite->rules = $rules + $wp_rewrite->rules; } add_action('generate_rewrite_rules', 'generate_taxonomy_rewrite_rules');
To clarify the following URLs work:
- domain.com/case-studies/page/2/ (this is the main post-type archive pagination)
- domain.com/case-studies/service_1/page/2/ (this is a taxonomy which is registered last)
And these 404:
- domain.com/case-studies/sector_1/page/2/ (this is a taxonomy which is registered first)
- domain.com/case-studies/location_1/page/2/ (this is a taxonomy which is registered second)
From the attempts I’ve made to fix this, I believe each of the rewrites (during the taxonomy registration) are overwriting those made registered before it. Because if I re-order the taxonomies that are registered the last one is always the one where pagination works correctly.
How can I set the taxonomy rewrites so that pagination works for each taxonomy?
- The topic ‘Pagination for custom taxonomy 404’ is closed to new replies.