1

I have this complex posts query, but I cant get the page navigation to work, been trying for hours with no luck, what am i missing?

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
    'post_type' => array('recipe','post'),
    'posts_per_page'  => 30,
    'paged' => $paged,
    'tax_query' => array(
          'relation' => 'OR',
       array(
         'taxonomy' => 'condition',
         'terms' => $arrArea1,
         'field' => 'slug'
       ),      
       array(
         'taxonomy' => 'diet_type',
         'terms' => 'High Fiber',
         'field' => 'slug'
       ),          
   )
);
$query = new WP_Query( $args );
?>  
<div class="postCont">   
    <?php if ( $query->have_posts() ) : ?>
        <h2>What's Fresh</h2>
        <?php while ( $query->have_posts() ) : $query->the_post(); ?>
            <?php get_template_part( 'content', 'profile-subscriber' ); ?>
        <?php endwhile; ?>
        <div class="navigation">
            <?php posts_nav_link(); ?>
        </div>
    <?php else:  ?>
        <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
    <?php endif; ?>
    <?php wp_reset_postdata(); ?>              
</div>    

I need to figure this out so I can do some ajax load more post actions, any help would be greatly appreciated

1 Answer 1

1

First of all, the post_nav_link() function is used to paginate the main query on home and archive pages. It does not work with custom queries (unless you hack the main query which I strongly discourage, it is the same as using query_posts then).

To paginate your custom query, you need to use

which will echo the links. If you need it returned, just use their get_* counterparts. There is a nice example of how to paginate a custom query on the next_posts_link() codex page.

I have also written a custom pagination function which work with any query and either display pagination as links or as numbers. You can check it out here

If you are going to using ajax for infinite scrolling, there are quite a few plugins (and tutorials IIRC) to check out and from which you can 'borrow' some code. Just make sure, I have seen one or two plugins/tutorials that uses query_posts. You should avoid using those as you should never ever use query_posts as it breaks/hacks the main query object.

One last note, I'm not sure where you would want to use the query, but static frontpages make use of get_query_var( 'page' ) and not get_query_var( 'paged' ), so if you are using the query on a static front page, make sure to change accordingly

0

Your Answer

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

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