In this post I’m going to explain how you can fix WordPress custom taxonomy pagination 404 error. Recently I had an issue with my custom post type taxonomy pagination. It was giving 404 “Page Not Found” when I tried to visit the second page by using the pagination like this:
My intention was to use my taxonomy pagination like as above and access pages by the following URL:
http://example.com/cities/city/page/2/
http://example.com/cities/city/page/3/
Here in this URL above, “cities” is my custom post type and “city” is my custom taxonomy. The reason it was not working is because WordPress slug rules weren’t updated for my custom taxonomy. So after creating the new rewrite rules it is now working just like I wanted. Now Let’s see how I come up and fixed this issue.
First of all set up how many posts you want to show on each page at “Settings => Reading” inside your WP Admin. After that add this codes below to your functions.php file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
function generate_taxonomy_rewrite_rules( $wp_rewrite ) { $rules = array(); $post_types = get_post_types( array( 'public' => true, '_builtin' => false ), 'objects' ); $taxonomies = get_taxonomies( array( 'public' => true, '_builtin' => false ), 'objects' ); foreach ( $post_types as $post_type ) { $post_type_name = $post_type->name; $post_type_slug = $post_type->rewrite['slug']; foreach ( $taxonomies as $taxonomy ) { 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; $rules[$post_type_slug . '/' . $term->slug . '/page/?([0-9]{1,})/?$'] = 'index.php?' . $term->taxonomy . '=' . $term->slug . '&paged=' . $wp_rewrite->preg_index( 1 ); } } } } $wp_rewrite->rules = $rules + $wp_rewrite->rules; } add_action('generate_rewrite_rules', 'generate_taxonomy_rewrite_rules'); |
This will rewrite the slug and enable pagination (/page/2) view for all of your taxonomies. Use WordPress default query loop as you don’t need to modify it anymore to show posts. Now that you know this should work on all of your post types. If you want to be specific with your custom post types just update these variables like this below:
1 2 3 |
$post_types = get_post_types( array( 'name' => 'cities', 'public' => true, '_builtin' => false ), 'objects' ); $taxonomies = get_taxonomies( array( 'name' => 'city', 'public' => true, '_builtin' => false ), 'objects' ); |
Enjoy 🙂
Awesome, thanks for the tip Jewel!
The solution didn’t work out of the box, I had to trouble shoot and replace the & on line 17 by an actual amptersand. My line became
$rules[$post_type_slug . ‘/’ . $term->slug . ‘/page/?([0-9]{1,})/?$’] = ‘index.php?’ . $term->taxonomy . ‘=’ . $term->slug . ‘&paged=’ . $wp_rewrite->preg_index( 1 );
You are absolutely correct! It’s the highlighter who did the trick. It has updated now. Thanks!
Very help full code
just copy and update pramalink working fine
Thank You sir
I change taxonomy slug for product_brand in brand in function.php file.
After that i give 404 in page.
how can i fix it?
Can you give me your codes so I can check? Also, give a try to re-install the theme again by selecting any default one and then your theme.
plz fix it
taxonomy=case-studies
post_type=casestudies
function generate_taxonomy_rewrite_rules( $wp_rewrite ) {
$rules = array();
$post_types = get_post_types( array( ‘public’ => true, ‘_builtin’ => false ), ‘objects’ );
$taxonomies = get_taxonomies( array( ‘public’ => true, ‘_builtin’ => false ), ‘objects’ );
foreach ( $post_types as $post_type ) {
$post_type_name = $post_type->name;
$post_type_slug = $post_type->rewrite[‘slug’];
foreach ( $taxonomies as $taxonomy ) {
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;
$rules[$post_type_slug . ‘/’ . $term->slug . ‘/page/?([0-9]{1,})/?$’] = ‘index.php?’ . $term->taxonomy . ‘=’ . $term->slug . ‘&paged=’ . $wp_rewrite->preg_index( 1 );
}
}
}
}
$wp_rewrite->rules = $rules + $wp_rewrite->rules;
}
add_action(‘generate_rewrite_rules’, ‘generate_taxonomy_rewrite_rules’);
Hello ,
Can you please let me know how to create pagination in the custom post type , display post content according to their taxonomy.
I recently searches some queries and found your site, really awesome work, keep it.
I can’t get this website to work oon my iphone properly
Is there a way to have this code also work for a taxonomy that has a hierarchical URL structure? Currently, it works if the Rewrite > Hierarchical is set to false.
Thank you.