Home > Enterprise >  Custom Taxonomy Pagination Not Functioning
Custom Taxonomy Pagination Not Functioning

Time:12-01

I have set up CPT with a custom taxonomy, yet I cannot get pagination working. First page renders fine but when I click through to "/page/2/", I get a 404 error.

I have a CPT named "species_guide" and a custom taxonomy named "species".

I attempted the solution outlined here: https://wpza.net/how-to-paginate-a-custom-post-type-in-wordpress/ and https://wpza.net/how-to-a-paginate-custom-taxonomy-archive-in-wordpress/ but to no avail.

Below is my code, any input would be appreciated:

in taxonomy-species.php I have the following:

        <?php

        $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
        $term_slug = get_query_var('species');
        $args = array(
                'post_type' => 'species_guide',
                'tax_query' => array(
                        array(
                            'taxonomy' => 'species',//custom taxonomy name
                            'field'    => 'slug',
                            'terms'    => $term_slug
                        )
                ),
                'posts_per_page' => 10,
                'paged' => $paged 
                );
        $loop = new WP_Query( $args );
            while ( $loop->have_posts() ) : $loop->the_post();
                    get_template_part( 'templates/post-archive' );
            endwhile;
?>
                <div >
     <?php
     $big = 999999999;
     echo paginate_links( array(
          'base' => str_replace( $big, '%#%', get_pagenum_link( $big ) ),
          'format' => '?paged=%#%',
          'current' => max( 1, get_query_var('paged') ),
          'total' => $loop->max_num_pages,
          'prev_text' => '&laquo;',
          'next_text' => '&raquo;'
     ) );

     wp_reset_postdata();
?>

and then in functions.php I have the following

function custom_tax_query_change( $query ) {
     if ( ! is_admin() && $query->is_tax( 'species' ) ) {
          $query->set( 'posts_per_page', 10 );
     }
}
add_action( 'pre_get_posts', 'custom_tax_query_change' );

Should I be using the different species slugs in the is_tax() function? Not sure why I can't get this working

CodePudding user response:

  1. Your taxonomy template name should be taxonomy-species.php to your match the taxonomy name, see WordPress documentation
  2. You don't need to create a new instance of WP_Query() for the loop, simply copy existing taxonomy.php or archive.php file and name the file as described above. Make HTML/PHP changes as needed.
  3. Here is the updated snippet for pagination:
<?php
// for total pages in the loop.
global $wp_query;

$big = 999999999;

echo paginate_links(
    array(
        'base'      => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
        'format'    => '?paged=%#%',
        'current'   => max( 1, get_query_var('paged') ),
        'total'     => $wp_query->max_num_pages,
        'prev_text' => '&laquo;',
        'next_text' => '&raquo;'
     )
);
?>
  • Related