wp_query->max_num_pages always returns 0 on custom post type

Hi there I am developing a theme where I have to load some posts with ajax, in my index page everything works properly, the problem is when I need to use it on custom post type, I pass the values for a jquery file using wp_localize_script, it pass the correct value when I have regular posts, but it always pass 0 when I have a custom post type. Can you help me? Many thanks.

Code to pass arguments to my jQuery script

function core_ajax_init() {

    global $wp_query;

    // Add code to index pages.
    if( !is_admin() ) { //!is_singular()

        // Enqueue jQuery Script to Process Ajax
        wp_enqueue_script(
            'core_custom',
            get_template_directory_uri(). '/core/js/ajax-load-posts.js',
            array('jquery'),
            '1.0',
            true
        );

        // What page are we on? And what is the pages limit?
        $max = $wp_query->max_num_pages;
        $paged = ( get_query_var('paged') > 1 ) ? get_query_var('paged') : 1;
        //echo $max;

        // Add some parameters for the JS.
        wp_localize_script(
            'core_custom',
            'core',
            array(
                'startPage' => $paged,
                'maxPages' => $max,
                'nextLink' => next_posts($max, false)
            )
        );
    }
 }

add_action('template_redirect', 'core_ajax_init');

The following is a template where I call to print the button to load more posts

<?php 
global $wp_query;

$found_posts = $wp_query->found_posts;
$per_page = get_option('posts_per_page');
$post_count = $found_posts - $per_page;

if($found_posts > $per_page) :
?>
<div class="row" id="load-more" data-order='999'>
    <div class="col-md-12">
        <div class="load-more-btn">
            <a id="load-more-btn" href="#">     
                <span id="detail-holder">
                    <div id="loader" data-perpage="<?php echo $per_page; ?>"></div>
                    <div class="load-more-text"><?php _e('Click here to load more', CORE_THEME_NAME);  ?></div>
                </span>
            </a>
        </div>
    </div> <!-- /.col-md-12 -->
</div> <!-- /.row -->
<?php endif; ?>

And here I have a snippet where I make a custom query for a specific post type

global $wp_query;
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$wp_query = new WP_Query(array( 'post_type' => 'course', 'order' => 'DESC', 'orderby' => 'date', 'paged' => $paged ));
if($wp_query->have_posts()): ?>

The problem here is it seems no matter how many pages I have wp_query var here always return 0 to my function core_ajax_init(), if I have regular posts it returns the correct number of pages but for custom post type it always returns 0. Why? Thank you for your answers.

$299 Affordable Web Design WordPress

This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

wp_query->max_num_pages always returns 0 on custom post type

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Schema.org Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.