1

I'm running into an issue where there are duplicate posts appearing in my paginated results. I suspect the issue is to do with my use of paginate_links(), but if anyone can see something obvious in my code, please let me know as I'm stuck. I've also included the code for my loop below which is used by archive-custom_post_type.php.

Examples

  • Searching for term 'magical' shows duplicate posts in the paginated results.
  • The main loop is also showing duplicate posts towards the end of the paginated results (it's a bit harder to see as there are lots of posts).

Code

// CHECK IF PAGINATION IS PRESENT
function osu_pagination_exists() {
    global $wp_query;
    return ($wp_query->max_num_pages > 1);
}

if(osu_pagination_exists()) : ?>
    <nav class="pagination clearfix">
        <?php // Numbered pagination
        global $wp_query;
        $big = 999999999; // need an unlikely integer
        echo paginate_links( array(
            'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
            'format' => '?paged=%#%',
            'current' => max( 1, get_query_var('paged') ),
            'total' => $wp_query->max_num_pages
        ) ); ?>
    </nav>
<?php endif; ?>

Main loop

<?php get_template_part( '../nav', 'postsnav' ); // TOP PAGINATION
if (have_posts()) : 
    while (have_posts()) : the_post(); ?>

        <h1 class="main-heading"><?php the_title(); ?></h1>

        <section class="audio-item">

            <h2><?php the_title(); ?></h2>

            <a href="<?php the_field('soundcloudlink'); ?>" class="sc-player audio-item--url"><?php the_title(); ?> on SoundCloud</a>

            <div class="audio-item--utility">
                ...audio player...
            </div> <!-- /audio-item--utility -->

            <article class="audio-item--meta">
                ...the content...
            </article>

        </section> <!-- /audio-item -->
    <?php endwhile;
else : ?>

    <div class="nada">
        <p>Nothing found</p>
    </div>

<?php endif; ?>
<?php get_template_part( '../nav', 'postsnav' ); // BOTTOM PAGINATION ?>
  • It's hard to find duplicate posts as there are lots of posts so please share the duplicate post name. Also please share your WP loop code which seems causing the issue. – Vinod Dalvi May 12 '17 at 5:21
  • Hi @Vinod Dalvi, thanks for getting back to me. It's difficult to pinpoint the duplicate posts as it seems like they sometimes some posts are duplicated when moving through the paginated pages and other times it will be different posts that are duplicated. Maybe you're right that the issue is the loop - I'll update my question above to include the loop now. – Osu May 12 '17 at 8:19
  • Just added the code for my loop above, but I don't think that's the issue as it's just some simple code - could it be that I'm calling the pagination before the main loop as well as after? – Osu May 12 '17 at 8:29
  • If that is the case then you can test it by removing either pagination. – Vinod Dalvi May 12 '17 at 8:49
  • Just trying that now to see if it's the issue. Meanwhile, could you try visiting the first link in my post (searching for the term magical) and look for the post titled 'Magic Land'. I'm seeing strange behaviour when flicking between the 1st and 2nd set of paginated results i.e. the order of results change slightly and Magic Land appears on both paginated results sometimes (although not always). I'm so confused by all this... – Osu May 12 '17 at 9:44

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.