I am currently trying to implement a load more function described in this stackoverflow post, and in order to do so it is necessary to know how many post have been loaded already. Thus, I am trying to return the number of current posts in a given page.

The code in question is the following:

 <div class="row">
            $posts = new WP_Query(array(
                'offset' => 0,
                'posts_per_page' => 6
            while ($posts->have_posts()) {
                $posts->the_post(); ?>

                <div class="card__wrapper col-sm-12 col-md-6 col-lg-4 ">
                    <?php get_template_part('partials/card', 'page'); ?>
            <?php }
            wp_reset_postdata(); ?>

        global $wp_query;
        // if ($wp_query->max_num_pages > 1)
            echo '<div class="row"><div class="col-sm-12"><div class="misha_loadmore">More posts</div></div></div>'; // you can use <a> as well

Even though, the WP_Query returns only 6 posts, the functions get_query_var('posts_per_page') and $wp_query->post_count, when called, return 10. Any ideas, what I am missing here?

Thanks in advance.

  • 1
    Your $posts references to a secondary/custom WordPress query, so you should use $posts->post_count and not $wp_query->post_count which is for the main query. – Sally CJ Mar 29 '20 at 10:43

Your Answer

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

Browse other questions tagged or ask your own question.