Jason
Jason

Reputation: 415

WordPress pagination not working displaying same set of posts

I am displaying posts and posts are limited to 10. And for rest, I have created pagination.

I have used a the_posts_pagination function. I used with and without arguments but my pagination is not working. It's always displaying the same set of posts

Please help me out.

Here is my code with pagination function I used.

<div class="row">
<?php
    $temp = $wp_query; $wp_query= null;
    $wp_query = new WP_Query(); $wp_query->query('cat=-1,-4,-12');
    while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
    <div class="col-md-12">
        <div class="single-blog-post">
            <div class="blog-post-cat"><?php the_category(', '); // Separated by commas ?></div>
            <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
            <div class="blog-meta"><?php the_time('F j, Y'); ?><?php the_author_posts_link(); ?></div>
            <div class="blog-details">
                <div class="row">
<div class="col-md-6">
    <div class="blog-thumbnail">
    <!-- post thumbnail -->
    <?php if ( has_post_thumbnail()) : // Check if Thumbnail exists ?>
            <?php the_post_thumbnail(); ?>
    <?php endif; ?>
    <!-- /post thumbnail -->
    </div>
</div>
<div class="col-md-6">
    <div class="blog-content">
        <?php  the_content(); ?>
        <a href="<?php the_permalink(); ?>" class="blog-readmore">Read More</a>
    </div>
</div>
                </div>
            </div>
        </div>
    </div>
    <?php endwhile; ?>
    <?php the_posts_pagination(); ?>
    <?php wp_reset_postdata(); ?>
</div>

Upvotes: 1

Views: 978

Answers (1)

user7236046
user7236046

Reputation:

You need to add the paged parameter to your query based on the current page you are on. From what I can see in the above code, you're looping through the posts but don't have an offset so the posts are always starting from 0.

Check out the WP Query documentation.

<div class="row">
<?php
    $temp = $wp_query; $wp_query= null;
    $wp_query = new WP_Query(); $wp_query->query( 'cat' => '-1,-4,-12', 'paged' => get_query_var( 'paged' ) );
    while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
    <div class="col-md-12">
        <div class="single-blog-post">
            <div class="blog-post-cat"><?php the_category(', '); // Separated by commas ?></div>
            <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
            <div class="blog-meta"><?php the_time('F j, Y'); ?><?php the_author_posts_link(); ?></div>
            <div class="blog-details">
                <div class="row">
<div class="col-md-6">
    <div class="blog-thumbnail">
    <!-- post thumbnail -->
    <?php if ( has_post_thumbnail()) : // Check if Thumbnail exists ?>
            <?php the_post_thumbnail(); ?>
    <?php endif; ?>
    <!-- /post thumbnail -->
    </div>
</div>
<div class="col-md-6">
    <div class="blog-content">
        <?php  the_content(); ?>
        <a href="<?php the_permalink(); ?>" class="blog-readmore">Read More</a>
    </div>
</div>
                </div>
            </div>
        </div>
    </div>
    <?php endwhile; ?>
    <?php the_posts_pagination(); ?>
    <?php wp_reset_postdata(); ?>
</div>

I've added the following to your query:

'paged' => get_query_var( 'paged' )

Try that out and it should display posts depending on the page you're currently on taking into account your actual query and arguments.

Upvotes: 2

Related Questions