Support » Fixing WordPress » Pagination for Custom Post Types

  • Resolved dreamnoir

    (@dreamnoir)


    I have successfully (though it took me hours to find a solution) got pagination for my custom post type, books. However, the pages linked, though the slugs are correct, give me 404 errors. I’ve tried everything, even changing the way pagination was achieved and flushing my rewrite rules several times.

    Example:
    http://www.thatrenee.com/books/ works fine, while
    http://www.thatrenee.com/books/page/2/ gives a very not so welcome 404 error.

    Is there anyone that has successfully implemented paging of custom post types so that the subsequent pages display the content correctly?

Viewing 15 replies - 1 through 15 (of 39 total)
  • Moderator keesiemeijer

    (@keesiemeijer)

    Is this a “Page”? if so, can you show us the query and loop of your theme’s page.php?

    Dreamnoir,

    Check out this post… it worked for me:

    http://wordpress.org/support/topic/374349

    @keesiemeijer It is using a custom page template to show the “posts” (I have the post_type’s capabilities set to post).

    <?php if (have_posts()) : ?>
    			<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; query_posts("post_type=books&posts_per_page=1&caller_get_posts=1&paged='. $paged .'"); ?>
    		<?php while (have_posts()) : the_post(); ?>
    
    ...regular post-displaying code here...
    
    		<?php endwhile; ?>
    			<?php if(function_exists('wp_pagenavi')) { wp_pagenavi(); } ?>
    		<?php else : endif; ?>

    @tpimental I still get the same 404 error 🙁

    Moderator keesiemeijer

    (@keesiemeijer)

    try this:
    change this:

    <?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; query_posts("post_type=books&posts_per_page=1&caller_get_posts=1&paged='. $paged .'"); ?>

    to this:

    <?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    query_posts('post_type=books&posts_per_page=1&caller_get_posts=1&paged='. $paged ); ?>

    And maybe to be sure go to “Settings” > “Permalinks” And click “Save Changes”. Sometimes you have to reset the permalinks for changes to take effect.

    Same result. It really doesn’t want to display my content on those subsequent pages. I’ve been at this for two days (almost continuous) and I’m about to throw in the towel and change it to display X number of recent books and let people navigate the rest in the aside menus 🙁

    Moderator keesiemeijer

    (@keesiemeijer)

    Change your permalinks to the default settings and see if this make’s it work. Or go to “Settings” > “Permalinks” And click “Save Changes”. Sometimes you have to reset the permalinks for changes to take effect.

    I did visit the permalinks page and clicked save changes, several times actually, after changing what you had mentioned. And it still gave me the 404 error. HOWEVER, changing it to the default and then back to what I had it at originally fixed it and now I can finally view the content on the illusive page/2! Thank you so much, I was about to give up 😀

    Moderator keesiemeijer

    (@keesiemeijer)

    never give up. glad it is working.

    I guess it just goes to show what someone can miss out on when they do give up 😀

    I had originally saw this problem resolved; however, now that I’ve started to add my books (from the Now Reading) plugin I find that the second page returns the 404 I received before.

    Things I have tried:
    * Made sure that the suggestions you made about changing the $paged information was correct and still intact.
    * Made sure that I had not changed something in my custom post type register that would cause it to not work.
    * Deleting .htaccess in the hopes that the new one WP created would fix it.
    * Refreshed and reset my permalink structure only about a thousand times in the hope that somehow it would stick.
    * Pulled my brains out and then reinserted them in the hopes that I just wasn’t thinking clearly enough about the problem.

    So far, nothing. Anyone have any fixes that have worked for them? It doesn’t appear to be anything I can find wrong with either WP or my “plugin” so should I cut my losses and just let the page say “last x books” and be done with it?

    Moderator keesiemeijer

    (@keesiemeijer)

    It is still working on my test site.
    Here is the code I used to create the post type

    add_action( 'init', 'create_post_type' );
    function create_post_type() {
      register_post_type( 'books',
        array(
          'labels' => array(
            'name' => __( 'books' ),
            'singular_name' => __( 'book' ),
            'rewrite' => array( 'slug' => 'books', 'with_front' => true ),
            '_builtin' =>  false, // It's a custom post type, not built in!
          ),
          'public' => true,
        )
      );
    }

    Maybe there is something in there that you can use for your post type

    The query_posts line should come before the if(have_posts() line.

    eg (based on your previously posted code).

    <?php
    $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
    query_posts( array( 'post_type' => 'books', 'posts_per_page' => 1, 'caller_get_posts' => 1, 'paged' => $paged ) );
    
    if (have_posts()) :
    
    	while (have_posts()) : the_post();
    	?>
    
    	...regular post-displaying code here...
    
    	<?php
    	endwhile; 
    
    	if(function_exists('wp_pagenavi'))
    		wp_pagenavi();
    
    else : 
    
    	// No posts
    
    endif;
    ?>

    I changed what I have to correctly reflect the order that if (have_posts()); goes in after the code, refreshed my permalinks, and still get the same thing.

    @kees, I checked it out and everything you have there is in my post-type declarations. Here is my template for the books.php page, if that helps anyone at all:

    <?php $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
    query_posts( array( 'post_type' => 'books', 'posts_per_page' => 5, 'caller_get_posts' => 1, 'paged' => $paged ) ); ?>
    		<?php if (have_posts()) : ?>
    		<?php while (have_posts()) : the_post(); ?>
                     ... code to display post here ...
    		<?php endwhile; ?>
    			<?php if(function_exists('wp_pagenavi')) { wp_pagenavi(); } ?>
    		<?php else : ?><?php endif; ?>

    Ok i see the issue (tested the code).

    The reason your page returns a 404 is because your post type occupies the URL format for /books/%bookname%/

    When you query your page /books/page/2 , WordPress assumes /page/ is a book with the name “page”, because of the rewrite rules that are setup for the post type..

    The rules look something like this (for reference)

    [books/[^/]+/attachment/([^/]+)/?$] => index.php?attachment=$matches[1]
    [books/[^/]+/attachment/([^/]+)/trackback/?$] => index.php?attachment=$matches[1]&tb=1
    [books/[^/]+/attachment/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$] => index.php?attachment=$matches[1]&feed=$matches[2]
    [books/[^/]+/attachment/([^/]+)/(feed|rdf|rss|rss2|atom)/?$] => index.php?attachment=$matches[1]&feed=$matches[2]
    [books/[^/]+/attachment/([^/]+)/comment-page-([0-9]{1,})/?$] => index.php?attachment=$matches[1]&cpage=$matches[2]
    [books/([^/]+)/trackback/?$] => index.php?book=$matches[1]&tb=1
    [books/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$] => index.php?book=$matches[1]&feed=$matches[2]
    [books/([^/]+)/(feed|rdf|rss|rss2|atom)/?$] => index.php?book=$matches[1]&feed=$matches[2]
    [books/([^/]+)/page/?([0-9]{1,})/?$] => index.php?book=$matches[1]&paged=$matches[2]
    [books/([^/]+)/comment-page-([0-9]{1,})/?$] => index.php?book=$matches[1]&cpage=$matches[2]
    [books/([^/]+)(/[0-9]+)?/?$] => index.php?book=$matches[1]&page=$matches[2]
    [books/[^/]+/([^/]+)/?$] => index.php?attachment=$matches[1]
    [books/[^/]+/([^/]+)/trackback/?$] => index.php?attachment=$matches[1]&tb=1
    [books/[^/]+/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$] => index.php?attachment=$matches[1]&feed=$matches[2]
    [books/[^/]+/([^/]+)/(feed|rdf|rss|rss2|atom)/?$] => index.php?attachment=$matches[1]&feed=$matches[2]
    [books/[^/]+/([^/]+)/comment-page-([0-9]{1,})/?$] => index.php?attachment=$matches[1]&cpage=$matches[2]

    You can work-around/fix this issue by setting the book query var / rewrite to “book” and not “books” when you register the post type, eg.

    'rewrite' => array( 'slug' => 'book' ),

    That way, all singular book queries are of the format.

    example.com/book/name

    And book listings/archives(your page) are of the format.

    example.com/books

    Just a conflict of interest between rewrite rules (i’d expect that to happen), but the above will resolve that problem (and i’ve tested it, so i know it works to).

    Hope that helps.. 🙂

    I think I love you. That worked perfectly! It’s funny how one “s” can mess things up 🙁 I’m very grateful for all of your help! I was not looking forward to working this problem out with the other post types I had planned on using later on 🙁

    You guys rule. Thanks so much!

Viewing 15 replies - 1 through 15 (of 39 total)
  • The topic ‘Pagination for Custom Post Types’ is closed to new replies.