Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Custom Post Types pagination not working WordPress

  • SOLVED

The problem is that the prev and/or next link are not showing up on my custom post type template. The pagination shows up well for regular posts. In reading section I have show up the first 2 posts and I have more events.

<strong>In my functions.php</strong>
//-----------register custom post type
register_post_type( 'events',
array(
'labels' => array(
'name' => __( 'Events' ),
'singular_name' => __( 'Event' ),
'add_new' => _x('Add New', 'event'),
'add_new_item' => __('Add New Event'),
'edit_item' => __('Edit Event'),
'new_item' => __('New Event'),
'view_item' => __('View Event'),
),
'public' => true,
'show_ui' => true,
'hierarchical' => false,
'capability_type' => 'post',
'rewrite' => 'events',
'query_var' => true,
'supports' => array( 'title', 'editor', 'excerpts', 'revisions')
)
);


<strong>In my Template file:</strong>

<?php get_header(); ?>

<?php $loop = new WP_Query( array( 'post_type' => 'events', 'orderby' => 'date', 'order' => 'ASC', 'paged' => $paged ) ); ?>
<?php if(have_posts()):
?>
loop here
<?php endwhile; ?>
<div id="posts_navigation">
<?php posts_nav_link(' ' , 'newer posts >>' , '<< older posts'); ?>
</div>
<?php endif; ?>

<?php get_footer(); ?>

Answers (2)

2010-10-18

Maor Barazany answers:

First, change

'rewrite' => 'events',
to
'rewrite' => array('slug' => 'events'),


Second - Currently you can't paginate a custom post type without a plugin if you use permalinks.
Only if you use the WP default, you can paginate.

That is a known issue on WP3.0 and 3.0.1, they just didn't create a them hierarchy file for custom posts type, and thus all of these issues.
I think I saw in WP Dev blog that they intend to extend the template hierarchy in WP3.1 to include also file for custom-post-type's archive page, and then it will be simple as having pagination to any other posts archives.
Till 3.1, the [[LINK href="http://wordpress.org/extend/plugins/simple-custom-post-type-archives/"]]Simple Custom Post Type Archives[[/LINK]] just do the job of having this template file hierarchy enabled and doing the correct rewrites that should be done for this to work.


After activating the plugin, create a template file to show your "archive" custom-post-type.
Take the archive.php (or index.php etc) from your theme, duplicate it and rename the file name to <strong>type-events.php</strong>.



Then, in the php file you created, find the loop (the line that starts with

if (have_posts()) : ?>

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




and just before it add this line:

query_posts('posts_per_page=10&paged='.$paged.'&post_type=events');


Lucian Florian comments:

Thank you Maor!

2010-10-18

Michael Fields answers:

This worked for me:

<?php
get_header();
query_posts( array(
'post_type' => 'events',
'orderby' => 'date',
'order' => 'ASC',
'paged' => $paged,
'posts_per_page' => 2
) );

if( have_posts() ) {
while ( have_posts() ) {
the_post();
the_title();
the_content();
}
}

print '<div id="posts_navigation">';
posts_nav_link( ' ' , 'newer posts >>' , '<< older posts' );
print '</div>';

get_footer();
?>


Lucian Florian comments:

The code did the trick but it returned error 404. After adding the plug-in mentioned by Maor yours worked too, but the template got messed up.