0

I have an archive-regularproducts.php set up that looks like so:
PHP:

    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    //content goes here
    <?php endwhile; endif; ?>

    <?php wp_reset_query(); ?>

    <?php query_posts( 'post_type=wpsc-product'); ?>
        <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
        //content goes here
        <?php endwhile; endif; ?>

    <?php wp_reset_query(); ?>

    <nav id="page_nav">
        <?php posts_nav_link( ' ', ' ', ' '); ?>
    </nav>

As you can see I'm also querying the wpsc-product post type on this archive page too, the posts display fine, but as I'm using infinite scroll, when it gets to the bottom and pulls in the next page of posts, it's pulling in duplicate wpsc-product posts, thus I was wondering if there was a way to either have the posts_nav_link function for only the regularproducts post type, or a generally better way of achieving what I'm trying to achieve. Any suggestions would be greatly appreciated!

1

2 Answers 2

1

query_posts should be avoided at all costs. This is not just my emphasis, but the codex's as well

Note: This function isn't meant to be used by plugins or themes. As explained later, there are better, more performant options to alter the main query. query_posts() is overly simplistic and problematic way to modify main query of a page by replacing it with new instance of the query. It is inefficient (re-runs SQL queries) and will outright fail in some circumstances (especially often when dealing with posts pagination).

You should also have a look at this post for further explanation

You should use WP_Query in this instance.

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
//content goes here
<?php endwhile; endif; ?>

<?php wp_reset_postdata(); ?>

<?php $my_query = new WP_Query( 'post_type=wpsc-product'); ?>
    <?php if ( $my_query->have_posts() ) : while ( $my_query->have_posts() ) : $my_query->the_post(); ?>
    //content goes here
    <?php endwhile; endif; ?>

<?php wp_reset_postdata(); ?>

<nav id="page_nav">
    <?php posts_nav_link( ' ', ' ', ' '); ?>
</nav>
0

Automatic pagination is only applied to the main query by default. Thus, the pagination is already working as it should. The problem probably lies in the way you're using InfiniteScroll.

IS allows you to target the container and items by a selector you specify. I would suggest wrapping the main query in a div and targeting that with Infinite Scroll. Are you using a plugin or implementing IS manually?

<div id="main">
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
        //content goes here
    <?php endwhile; endif; ?>
</div>

<?php wp_reset_query(); ?>

<?php query_posts( 'post_type=wpsc-product'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    //content goes here
<?php endwhile; endif; ?>

<?php wp_reset_query(); ?>

<nav id="page_nav">
    <?php posts_nav_link( ' ', ' ', ' '); ?>
</nav>

Then, you should target the container #main and all post items inside it.

6
  • query_posts should not be used. Here is an explanation Commented Jun 9, 2014 at 7:08
  • I'm very aware of that, but that's not within the scope of this question as it's not the cause of the problem.
    – engelen
    Commented Jun 9, 2014 at 7:10
  • Even though this is not in the scope of the question, you should mention this to the OP and amend your code accordingly. Remember, if someone finds your answer via google, they will use your solution, not knowing that it is wrong. The intention of an answer is not just about a solution to a specific aspect, and ignoring other wrongs that is not in scope Commented Jun 9, 2014 at 7:15
  • I disagree. When answering general questions applicable to multiple or general problems, it is quite important to not only help the OP but also ensure that if the original code is adjusted, it is also adjusted to adhere to the coding standards and, in general, be considered "good code". In this case, however, the problem is most likely limited to just the OP's case, and focusing on side issues like this will only serve to make answers less understandable. In this case, helping the OP is more important than providing insight in the proper usage of WordPress.
    – engelen
    Commented Jun 9, 2014 at 7:38
  • What tickles me, if you know the OP is using a function that should not be used, or a function that is depreciated, or code that have a syntax error, you would not correct him, because it is out of scope of his original question? Commented Jun 9, 2014 at 7:46

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.