Pagination on custom taxonomy

I’m having a tough time getting this to work. It’s not the first time I run into problems with pagination, or I would say with WPs URL system.

Basically I have this URL:

Read More
http://example.com/location/dc 

and loads up the taxonomy-location.php template.

Now, I’m adding the pagination feature to the theme. So I have this URL:

http://example.com/location/dc/page/2

and it won’t load the taxonomy-location.php template, it actually loads the 404 template.

This is what the debug bar shows:

http://cl.ly/1u0F470k062o1Q121u15

It seems like it’s getting the right values, WP is just not loading up the correct template.


EDIT (Moved over from the comment links)

functions.php

/* LOCATION */
add_rewrite_rule( 'location/([^/]+)/page/([0-9]{1,})/?$', 'index.php?location=$matches[1]&paged=$matches[2]', 'top' );

taxonomy-location.php

  <?php 
  $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
  query_posts($query_string.'&posts_per_page=2&paged='.$paged);
  ?>

  <h2>"<?= ucwords(get_query_var('location')); ?>" Venues</h2>

  <?php if (have_posts()) : ?>

    <?php while (have_posts()) : the_post(); ?>

  <article>
    <h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
    <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('venue_thumb'); ?></a>
  </article>

  <?php endwhile; ?>

    <?php wp_pagenavi(); ?>

  <?php else : ?>

    <article>
      <h1>No posts found</h1>
    </article>

<?php endif; ?>

Related posts

Leave a Reply

3 comments

  1. Try adding 'paged' => get_query_var( 'page' ) to your query.

    EDIT : For pagination to work properly the posts per page should be greater than ‘Blog pages show at most’ under Settings-> Reading section of the WordPress Admin. So there are two ways you can make this work out.

    1. You can set ‘Blog pages show at most’ to 1. This would show 1 post in all the blog pages unless you specify post_per_pages specifically for each query.
    2. The other option you have would be to use the pre_get_posts filter.

    Example of pre_get_post filter to limit the no. of posts on a location taxonomy archive page to 2 posts per page.

    function location_posts( $query ) {
        if( is_tax( 'location' ) ) {
            $query->set('posts_per_page', '2');
        }
        return $query;
    }
    add_filter('pre_get_posts', 'location_posts');
    
  2. register_post_type( 'lifestyle',
                    array( 
                    'label' => __('Lifestyle', 'tmi'), 
                    'public' => true, 
                    'show_ui' => true,
                    'show_in_nav_menus' => true,
                    'rewrite' => true,
                    'hierarchical' => true,
                    'menu_position' => 5,
                    'exclude_from_search' =>false,
                    'supports' => array(
                                         'title',
                                         'editor',
                                         'thumbnail',
                                         'excerpt',
                                         'revisions')
                        ) 
                    );
    
        register_taxonomy('lifestylecat', __('lifestyle', 'tmi'),array('hierarchical' => true, 'label' =>  __('Categories', 'tmi'), 'singular_name' => __('Category', 'tmi'))
        );
    
  3. I was running into the same issue.

    Here is what I added to my functions.php to resolve the issue.

    add_action(
        'pre_get_posts',
        function( $query ) {
            // When creating custom Rewrites we lose the paged number so we need to set it here.
            if ( preg_match( '//page/([0-9]+)/?/', ( ! empty( $_SERVER['REQUEST_URI'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : '' ), $matches ) ) {
                if ( ! empty( $matches[1] ) && is_numeric( $matches[1] ) ) {
                    set_query_var( 'paged', $matches[1] );
                }
            }
            return $query;
        }
    );
    
    add_action(
        'init',
        function() {
            add_rewrite_rule(
                'location/(.*)/page/([0-9]+)/?',
                'index.php?page=$matches[2]&taxonomy=location&location=$matches[1]',
                'top'
            );
        }
    );
    

    Notice that in the rewrite you must use page= and not paged=. Not sure why you can’t use paged there.

    Make sure you flush your rewrite rules afterward. (Re-save Permalinks)