1

For some reason, I have posts that keep repeating on certain pagination pages using the code below. It's completely random, so I have no idea how to fix the problem. Maybe it's something in the post itself that's causing it to be repeated?

Some examples of the problem can be found here:

<?php if(have_posts()) : while(have_posts()) : the_post(); ?>   

<div class="blogcontent"> 
    <div class="post_img"><a href="<?php the_permalink(); ?>">
    <?php $img = wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); ?>
    <?php $image_alt = get_post_meta( get_post_thumbnail_id($post->ID), '_wp_attachment_image_alt', true); ?>
                <?php if($img) { ?>
                    <img src="<?php echo $img; ?>" alt="<?php echo $image_alt; ?>">
                <?php } ?></a></div>
    <div class="post_content">
    <?php 
        $category_detail=get_the_category($post->ID);//
            foreach($category_detail as $cd){ ?>

                <a href="<?php echo get_home_url(); ?>/category/<?php echo $cd->slug; ?>/"><span class="boldtag"><?php echo $cd->cat_name; ?></span></a>
        <?php       }
    ?>

        <h1><a href="<?php the_permalink(); ?>"><?php the_title();?></a></h1>
        <?php the_excerpt(); ?>
        <ul class="author clearfix">
            <li>By <em><?php the_author_posts_link(); ?></em> <span>· <?php the_time('M d, Y') ?></span></li>
            <li><a href="<?php the_permalink(); ?>">MORE...</a></li>
        </ul>
    </div>
</div>
<?php endwhile; ?>

<div class="paginationsec">
    <?php // Previous/next page navigation.
        the_posts_pagination( array(
            'prev_text'          => __( '<i class="fa fa-angle-double-left"></i> Prev', 'twentyfifteen' ),
            'next_text'          => __( 'Next <i class="fa fa-angle-double-right"></i>', 'twentyfifteen' ),
            'before_page_number' => '<span class="meta-nav screen-reader-text">' . __( '', 'twentyfifteen' ) . ' </span>',
        ) ); ?>

</div>

<?php else: endif; ?>

10
  • No need to use "if(have_posts()) "
    – Ali Ali
    Commented Apr 21, 2020 at 18:03
  • Did you try disabling all plugins to see if the problem persists? When did you start noticing the duplicate posts?
    – Himad
    Commented Apr 21, 2020 at 18:27
  • It's persisted before plugins were even installed. The original developer couldn't figure it out either.
    – jpollar
    Commented Apr 21, 2020 at 18:28
  • If you run a WP_Query() and print its results, do you get duplicate posts?
    – Himad
    Commented Apr 21, 2020 at 18:41
  • 1
    I figured out what's causing the problem, I just don't understand why. I have a bunch of old posts that were published on the exact date and time on March 26, 2018. When I change the publish date for posts that repeat, it no longer repeats it in the navigation. Why would a post published date-timestamp cause it to repeat in navigation????
    – jpollar
    Commented Apr 21, 2020 at 20:23

1 Answer 1

4
+100

Try with a custom WP_Query to rule out any possible code messing up with your main query pagination. Like this:

<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$query = new WP_Query( array( 'post_type' => 'post', 'post_status' => 'publish', 'posts_per_page' => 5, 'paged'=> $paged) );

?>
<?php while($query->have_posts()) : $query->the_post(); ?>   

<div class="blogcontent"> 
    <div class="post_img"><a href="<?php the_permalink(); ?>">
    <?php $img = wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); ?>
    <?php $image_alt = get_post_meta( get_post_thumbnail_id($post->ID), '_wp_attachment_image_alt', true); ?>
                <?php if($img) { ?>
                    <img src="<?php echo $img; ?>" alt="<?php echo $image_alt; ?>">
                <?php } ?></a></div>
    <div class="post_content">
    <?php 
        $category_detail=get_the_category($post->ID);//
            foreach($category_detail as $cd){ ?>

                <a href="<?php echo get_home_url(); ?>/category/<?php echo $cd->slug; ?>/"><span class="boldtag"><?php echo $cd->cat_name; ?></span></a>
        <?php       }
    ?>

        <h1><a href="<?php the_permalink(); ?>"><?php the_title();?></a></h1>
        <?php the_excerpt(); ?>
        <ul class="author clearfix">
            <li>By <em><?php the_author_posts_link(); ?></em> <span>· <?php the_time('M d, Y') ?></span></li>
            <li><a href="<?php the_permalink(); ?>">MORE...</a></li>
        </ul>
    </div>
</div>
<?php endwhile; ?>
<div class="paginationsec">
  <nav class="navigation pagination show">
    <div class="nav-links">
    <?php 
        echo paginate_links( array(
            'base'         => str_replace( 999999999, '%#%', esc_url( get_pagenum_link( 999999999 ) ) ),
            'total'        => $query->max_num_pages,
            'current'      => $paged,
            'format'       => '?page=%#%',
            'show_all'     => false,
            'type'         => 'plain',
            'end_size'     => 5,
            'mid_size'     => 1,
            'prev_next'    => true,
            'prev_text'    =>__( '<i class="fa fa-angle-double-left"></i> Prev', 'twentyfifteen' ),
            'next_text'    => __( 'Next <i class="fa fa-angle-double-right"></i>', 'twentyfifteen' ),
            'before_page_number' => '<span class="meta-nav screen-reader-text">' . __( '', 'twentyfifteen' ) . ' </span>',
            'add_args'     => false,
            'add_fragment' => '',
        ) );
    ?>
    </div>
  </nav>

</div>
1
  • Thank you!!! I appreciate your time and effort!
    – jpollar
    Commented Apr 22, 2020 at 14:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.