0

I'm able to return the correct page numbers (1 post per page) but the paginated links keep returning a 404 page. Here's the function I'm calling for the pagination:

//Pagination for Custom Taxonomy Terms
function custom_page_navi( $totalpages, $page, $end_size, $mid_size )
{
    $bignum = 999999999;

    if ( $totalpages <= 1 || $page > $totalpages ) return;

        //NOTE: https://codex.wordpress.org/Function_Reference/paginate_links
    return paginate_links( array(
        'total'         => $totalpages,
        'prev_next'     => false,
        'type'          => 'list',
        'show_all'      => false,
        'end_size'      => $end_size,
        'mid_size'      => $mid_size
    ) );

}

Here is code I'm using for within my taxonomy-page-name.php file:

      <?php

$this_page_taxonomy = $wp_query->get_queried_object(); //getting this page terms
$current_slug       = $this_page_taxonomy->slug; //current slug
$time               = current_time('timestamp'); //current time
$go_cpt             = 'name'; //taxonony  name
$number             = 1; // number of terms to display per page


// Setup for pagination:
$page       = (get_query_var('paged')) ? get_query_var('paged') : 1;
$offset     = ($page > 0) ? $number * ($page - 1) : 1;
$totalposts = $this_page_taxonomy->count;
$totalpages = ceil($totalposts / $number);

$go_args = array(
    'post_type' => $go_cpt,
    'orderby' => 'meta_value',
    'meta_key' => 'go_series_date',
    'meta_value' => $time,
    'meta_compare' => '>=',
    'order' => 'ASC',
    'exclude' => array(),
    'exclude_tree' => array(),
    'include' => array(),
    'posts_per_page' => $number,
    'fields' => 'all',
    'slug' => '',
    'parent' => '',
    'hierarchical' => true,
    'child_of' => 0,
    'get' => '',
    'name__like' => '',
    'pad_counts' => false,
    'offset' => $offset,
    'search' => '',
    'cache_domain' => 'core',
    'tax_query' => array(
        array(
            'taxonomy' => 'go_series',
            'field' => 'slug',
            'terms' => $current_slug
        )
    )
);



$go_recent_posts = new WP_Query($go_args);

if ($go_recent_posts->have_posts()):
    while ($go_recent_posts->have_posts()):
        $go_recent_posts->the_post();

        printf('<nav class="recent-series__pagination">%s</nav>', custom_page_navi($totalpages, $page, 3, 0));
    endwhile;
endif;

?>

Is it potentially a rewrite problem?? Not sure what's going on here...Thanks!

1

First things first, you're throwing away the default query for the page (after it hits the DB and sets up the globals) with your custom query. The pagination is referring to the default query for the template, which is defined in Settings > Reading, plus you're hitting the DB twice for the same thing, which is hideously inefficient.

If you want to change the options of the main query for a template you should hook into pre_get_posts and modify the default query before it's sent to the DB, like so:

add_action('pre_get_posts', 'wpse276456_custom_tax_query');

function wpse276456_custom_tax_query($query) {
  if(is_tax('page-name') {
     $query->set('posts_per_page', 1);
     // Add more calls to $query->set as necessary
  }
}
  • thanks! Rather than using the default query, can I not use my custom query for pagination though? – bhood Aug 10 '17 at 12:26
  • Adding a custom query should only be used for supplementary Loops on the same page, if you want to alter what's displayed for the main query hooking to pre_get_posts and altering it is the correct method. – Chris Cox Aug 10 '17 at 12:29
  • That was it! Thanks!! Post as an answer when you get a momentt – bhood Aug 10 '17 at 13:12
  • It already is... – Chris Cox Aug 10 '17 at 13:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.