Support » Fixing WordPress » Posts Duplicating due to Pagination and Custom Query

  • I have a custom meta field named “price” and I would like my main query to order the posts from highest to lowest price.

    Currently I have it setup like this:

    <?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    query_posts ("orderby=meta_value_num&meta_key=price&order=DESC&paged=".$paged); ?>
        <?php if ( have_posts() ) : ?>
            <?php while ( have_posts() ) : the_post(); ?>
                    get_template_part( 'content', get_post_format() );
            <?php endwhile; ?>
            <?php paging_nav(); ?>
        <?php else : ?>
            <?php get_template_part( 'content', 'none' ); ?>
        <?php endif; ?>

    The issue I am running into is having a post duplicate when going to the second page

    Let’s say I have 6 posts in total and I have it setup to load 3 posts at a time.

    1. $100
    2. $90
    3. $50
    4. $50
    5. $25
    6. $10

    It will load the first three posts no problem and in order by price descending. However when I go to the next page to get the next three posts, Post number 3 would sometimes repeat itself as post number 4 and the original post number 4 would not appear and 5 and 6 would follow like intended.

    How do I prevent the query from repeating post number 3 just because it have the same price value as post number 4 when I go to the next page?

    Note this only happens about 50% of the time and the order is usually correct. If post number 3 and 4 were different values, this issue would not happen and it would order correctly 100% of the time. The issue lies in the fact that they are the same value and are separated by pagination since I have it setup to only load 3 at a time. When retrieving the second page of posts, the query does not register the fact it already used post 3 and duplicates it as post 4 seeing they have the same price value.

    I have tried the “pre_get_posts” function as well as the “new Wp_Query” function, the problem still persists.

Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘Posts Duplicating due to Pagination and Custom Query’ is closed to new replies.