How To Fix WordPress custom taxonomy pagination 404 error

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:

pagination_demo-300x40

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:

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:

Enjoy 🙂

10 thoughts on “How To Fix WordPress custom taxonomy pagination 404 error”

  1. 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 );

    1. 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.

      1. 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’);

  2. Hello ,

    Can you please let me know how to create pagination in the custom post type , display post content according to their taxonomy.

  3. 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.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.