Search code examples
phpwordpresscustom-post-type

$wp_query->max_num_pages not working for CPT


am implementing custom pagination for my CPT products. Here what i tried -- in taxonomy-product-category.php

<?php $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1; 
    $args = array(
        'post_type' => 'products',
        'post_status' => 'publish',
        'posts_per_page' => 6,
        'parent' => 0,
        'paged' => $paged,
        'orderby' => 'date',
        'order' => 'DESC',
        'tax_query' => array(
            array(
                'taxonomy' => $taxonomy,
                'field' => 'slug',
                'terms' => array($term->slug),
                'operator' => 'IN'
            ),
        ),
    ); 
?>

in functions.php

 function get_pagination($range = 4) {
    global $paged, $wp_query;
    $no_of_posts = 6;
    // How much pages do we have?
    if (!$max_page) {
        $max_page = round($wp_query->max_num_pages / $no_of_posts);
    }
    die($wp_query->max_num_pages);
}

But the issue is

if (!$max_page) {
    $max_page = round($wp_query->max_num_pages / $no_of_posts);
}
die($wp_query->max_num_pages);

For this it is always returning $wp_query->max_num_pages always returning 1.I don't want to divide it by number of posts .


Solution

  • There are actually a few issues that can cause this but let's start with the most frequent one:

    You probably have some other filter that intervene with the query in early stage, something like:

    add_action('pre_get_posts', 'some_function');
    

    or any other function that alters the query. Try to remove / change priority and see results but stay alert because this pre_get_post action might be neccessary / usefull for other things, so be sure you know what you are doing..