I am working on a Wordpress project where I use the Posts 2 Posts plugin to show posts from one post type to posts from another post type.

This works great but for some reason my next_posts_link() and previous_posts_link() doesn't show up.

I have specified the paged variable in my code so it should work.

Can anyone see what I am doing wrong?

$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
$connected = new WP_Query( array(
    'connected_type' => 'services_to_companies',
    'connected_items' => get_queried_object(),
    'paged' => $paged,
    'posts_per_page' => 3

if ( $connected->have_posts() ) { ?>

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

<div class="company_preview box_shadow">        
    <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>          
</div><!--End company_preview-->

<?php endwhile; ?>

<?php if ( $connected->max_num_pages > 1 ) {
$current_page = $paged;
$total_pages = $connected->max_num_pages;

<div id="nav-above">
    <div class="nav-next bx-next"><?php next_posts_link( 'Next &rarr;' ); ?></div>
    <div class="number"><?php echo $current_page . ' / ' . $total_pages; ?></div>
    <div class="nav-previous bx-prev"><?php previous_posts_link( '&larr; Previous' ); ?></div>
<?php } ?>

<?php wp_reset_postdata(); } ?>
  • Have you read the Codex page for next_posts_link? – Milo Mar 19 '14 at 15:13
  • Yes I did. But cannot find the problem. – Robbert Mar 20 '14 at 8:30

From next_posts_link:

Add the $max_pages parameter to the next_posts_link() function when querying the loop with WP_Query. To get the total amount of pages you can use the 'max_num_pages' property of the custom WP_Query object.

next_posts_link( 'Next &rarr;', $connected->max_num_pages );
  • Yes I have tried that, but doesn't work. The strange is that if I do /?page=2 nothing happens.. – Robbert Mar 21 '14 at 7:51
  • page and paged are not the same. what type of page do you have this code on? regardless, if you don't pass max_num_pages, it uses the value from an entirely different query. – Milo Mar 21 '14 at 18:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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