3

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.

  • 2
    can you show us your code? – Milo Mar 12 '14 at 4:30
  • Yeah I will share the code where I call ajax load, I will upload my question with the relevant code snippet. – mviniciusbarreto Mar 13 '14 at 2:28
  • where is that last snippet located? it appears you're reading the max num pages argument from a different query (the main query) and not your custom query. – Milo Mar 13 '14 at 2:49
  • Hi thank you for your reply, last snippet is located in the template for my custom post type, it seems my function core_ajax_init only read the main query for regular posts and not custom queries in custom post type. – mviniciusbarreto Mar 13 '14 at 12:59
  • 1
    OFFTOPIC: enqueue callbacks should be hooked to wp_enqueue_scripts for front end and admin_enqueue_scripts for back end, not to template_redirect. – Abhik Nov 1 '15 at 18:30
0

Yuo can also do it this way, count published posts, get settings posts per page, then divide.

$published_posts = wp_count_posts()->publish;
$posts_per_page = get_option('posts_per_page');
$page_number_max = ceil($published_posts / $posts_per_page);

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.