next_post_link() with custom post types

I have no idea why next_post_link() and previous_post_link() doesn’t work on my custom post type:



<?php next_post_link(); ?>	

<?php if ( have_posts() ) : ?>

	<?php while ( have_posts() ) : the_post(); ?>

		

<?php the_title(); ?>

<?php endwhile; ?> <?php else : ?> <?php endif; ?> <?php previous_post_link(); ?>

Any ideas?

Thought I had done this successfully before, but turns out it doesn’t work either.

Maybe this? http://wordpress.org/support/topic/pagination-with-custom-post-type-listing

Yep, I found that thread (Google is my friend) but it wont work with @rafaelxy approach :frowning:

revaxarts said

Yep, I found that thread (Google is my friend) but it wont work with @rafaelxy approach :frowning:

Try this:

    
<?php previous_posts_link(__( 'Previous entries', 'anariel')); ?> <?php next_posts_link(__( 'Next posts', 'anariel')); ?>

I have this inside mine portfolio custom page and it works, adjust margins for u.

duplicate

anariel7 said

Try this:

    
<?php previous_posts_link(__( 'Previous entries', 'anariel')); ?> <?php next_posts_link(__( 'Next posts', 'anariel')); ?>

I have this inside mine portfolio custom page and it works, adjust margins for u.

previous_posts_link() returns null so no need to set margins to nothing :wink:

Hey revaxarts,

I used something like this, for a Portfolio Post Type, in single-portfolio.php:

<?php if( get_next_post() ) : ?>
      
<?php next_post_link('%link → ') ?>
<?php endif; ?> <?php if( get_previous_post() ) : ?>
<?php previous_post_link('← %link |') ?>
<?php endif; ?>

Hope it helps.

Paul

@icypixels get_next_post() is always null so the condition is always false.

Any other ideas?

revaxarts said

@icypixels get_next_post() is always null so the condition is always false.

Any other ideas?

The previous_post_link(); and next_post_link(); tags MUST be used within the loop and you’ve added them outside of the loop…put it before <?php endif; ?> and put the next_post_link(); inside the loop after if ( have_posts() ) : .

WPExplorer said

The previous_post_link(); and next_post_link(); tags MUST be used within the loop and you’ve added them outside of the loop…put it before <?php endif; ?> and put the next_post_link(); inside the loop after if ( have_posts() ) : .

Yes I thought about that - without success :frowning:

Maybe here is something wrong:

register_post_type( 'portfolio', array(
	
	'labels' => array( /* blabla */),
	'public' => true,
	'can_export' => true,
	'show_ui' => true,
	'show_in_nav_menus' => false,
	'menu_position' => 30,
	'has_archive' => false,
	'hierarchical' => false,
	'rewrite' => true,
	'supports' => array( 'title' ),
	'register_meta_box_cb' => array( $this, 'add_meta_boxes' ) 
);

I thought about the rewrite option and tried everything without success (always called flush_rewrite_rules() )

'has_archive' => false

shouldnt this be true?

Your question is not clear for me… Is it for your “post type” archive page or for single “post type” page page?

For archive page, archive-yourposttype.php, or your custom page template to show your custom post type, you have to use next_posts_link and previous_posts_link inside the loop.

References:

http://codex.wordpress.org/Function_Reference/next_posts_link

http://codex.wordpress.org/Function_Reference/previous_posts_link

For single post page, single-yourposttype.php, you have to use next_post_link and previous_post_link inside the loop.

References:

http://codex.wordpress.org/Function_Reference/next_post_link

http://codex.wordpress.org/Function_Reference/previous_post_link

Note: we use them on one of our theme and it works. :wink:

Thanks to all who helped me but the solution is quite different!

I didn’t get the mentioned links because I changed the post status from ‘published’ to ‘finished’ and the get_adjacent_post() function in the link-template.php file generates a SQL statement with post_status = ‘publish’. Fortunately it provides a filter hook so this code solves my problem:

add_filter('get_previous_post_where', 'my_get_post_where');
add_filter('get_next_post_where', 'my_get_post_where');

function my_get_post_where($sql) {
	return str_replace("'publish'", "'finished'", $sql);
}

I think the register_post_status is not implemented well in the whole WP core

This is more simple and clean:

<?php $prev_post = get_previous_post(); ?> <?php if ( !empty( $prev_post ) ) : ?> <?php endif; ?> <?php $next_post = get_next_post(); ?> <?php if ( !empty( $next_post ) ) : ?> <?php endif; ?>