WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Pagination for Custom Post Types (40 posts)

  1. dreamnoir
    Member
    Posted 3 years ago #

    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?

  2. keesiemeijer
    moderator
    Posted 3 years ago #

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

  3. tpimental
    Member
    Posted 3 years ago #

    Dreamnoir,

    Check out this post... it worked for me:

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

  4. dreamnoir
    Member
    Posted 3 years ago #

    @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 :(

  5. keesiemeijer
    moderator
    Posted 3 years ago #

    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.

  6. dreamnoir
    Member
    Posted 3 years ago #

    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 :(

  7. keesiemeijer
    moderator
    Posted 3 years ago #

    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.

  8. dreamnoir
    Member
    Posted 3 years ago #

    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 :D

  9. keesiemeijer
    moderator
    Posted 3 years ago #

    never give up. glad it is working.

  10. dreamnoir
    Member
    Posted 3 years ago #

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

  11. dreamnoir
    Member
    Posted 3 years ago #

    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?

  12. keesiemeijer
    moderator
    Posted 3 years ago #

    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

  13. Mark / t31os
    Moderator
    Posted 3 years ago #

    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;
    ?>
  14. dreamnoir
    Member
    Posted 3 years ago #

    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; ?>
  15. Mark / t31os
    Moderator
    Posted 3 years ago #

    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.. :)

  16. dreamnoir
    Member
    Posted 3 years ago #

    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!

  17. Mark / t31os
    Moderator
    Posted 3 years ago #

    You're welcome.. :)

  18. Harish Chouhan
    Member
    Posted 3 years ago #

    Mark, you are awesome, genuis guys. I wish I had your brains. I spent last 3 days trying to fix the same issue as dreamnoir had on my website http://www.dreamsmedia.in

    Finally am able to solve due to that plural/singular thing.

    You are just great. if you have any twitter account, please let me know so I can follow you.

  19. Harish Chouhan
    Member
    Posted 3 years ago #

    Mark, need your help.

    My custom post type name is work.
    The page which displays all post inside this is http://www.dreamsmedia.in/work/

    Now when I try the individual work post to appear as http://www.dreamsmedia.in/work/work-post-name/ it appears properly but then the pagination on http://www.dreamsmedia.in/work/ (like http://www.dreamsmedia.in/work/page/2/) does not work.

    So I used your above method and changed the slug of custom post type to "project" and now the work posts appear as:
    http://www.dreamsmedia.in/project/work-post-name/

    But is there a way to make 2nd pages of http://www.dreamsmedia.in/work/page/2 not through 404 errors and still make the work post link as http://www.dreamsmedia.in/work/work-post-name/?

    If this is possible, would really appreciate it

  20. Mark / t31os
    Moderator
    Posted 3 years ago #

    Harish, can you start a topic describing your problem, with details of how your post type is registered, what pages you have setup etc.. please.. rather then posting onto the end of a resolved topic (alot of users won't check resolved topics).

    NOTE: You cannot have pages with the same name as that of a slug from a custom post type, else you have two sets of conflicting rewrite rules, which was the problem discussed above.

  21. Harish Chouhan
    Member
    Posted 3 years ago #

    Hello Mark,

    Sorry was new here realized it later. The problem got solved. On another post in this forum, I found the same thing you mentioned about slug name being different.
    Thanks again.

  22. lost_in_wp
    Member
    Posted 3 years ago #

    Hi,

    Not sure if this is related, but I'm having problems getting pagination working on the authors page. The complication may be that I need the author's page to list only the custom post type 'advert'.

    So, in author.php I have:

    if(isset($_GET['author_name']))
        $curauth = get_user_by('slug', $author_name);
    else
        $curauth = get_userdata(intval($author));
    
    $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
    
    query_posts( array( "post_type" => "advert", "paged" => $paged, "author" => $curauth->ID ) );

    In the page 'next_posts_link()' returns /author/admin/page/2 but this link 404s. With permalinks turned off the url ?author=1&paged=2 also returns 404.

    Custom post type registered with:

    $args = array(
    		'labels' => $labels,
    		'public' => true,
    		'publicly_queryable' => true,
    		'show_ui' => true,
    		'query_var' => true,
    		'rewrite' => array('slug' => 'ads'),
    		'capability_type' => 'post',
    		'hierarchical' => false,
    		'menu_position' => null,
    		'supports' => array('title', 'editor', 'author', 'custom-fields', 'revisions'),
    	); 
    
    register_post_type( 'advert', $args );

    Any ideas greatly appreciated!

  23. Mark / t31os
    Moderator
    Posted 3 years ago #

    Your query might have been better suited in it's own thread, this topic is resolved and won't likely get much attention simply due to the fact it's considered resolved.

    That said, if you can provide a link to your site, i'll do what i can to help..

    Q: What permalink structure are you using?

  24. lost_in_wp
    Member
    Posted 3 years ago #

    Hi Mark,

    Thanks. I'm afraid the site is not public though but the permalink structure is: /%year%/%monthnum%/%postname%/ and both category and tag bases are blank (default).

  25. Mark / t31os
    Moderator
    Posted 3 years ago #

    If you remove the query_posts line does the code work?

    query_posts( array( "post_type" => "advert", "paged" => $paged, "author" => $curauth->ID ) );

    If that doesn't work, then you have another problem with paging and it's not a result of your custom code (would be handy to know before suggesting changes to the code).

    Can you confirm the above please.

  26. lost_in_wp
    Member
    Posted 3 years ago #

    Yes, paging does then work correctly but only lists "post" post types not my custom post type.

  27. Mark / t31os
    Moderator
    Posted 3 years ago #

    Just wanted to confirm if the problem was your query_posts call... ;)

    Try a different approach?

    $args = array( "post_type" => "advert", "paged" => $paged, "author" => $curauth->ID );
    $args = array_merge( $args, $wp_query->query ); // Merge with the existing query vars
    query_posts( $args );
  28. lost_in_wp
    Member
    Posted 3 years ago #

    Hehe, yes, that worked - partly, but this is a bit strange - this author has 7 'adverts', if I set posts per page (from Reading Settings) to 10 then I correctly get all 7 on the author page. If I change it to 2 per page I get the first two and the next and previous links appear, I then move to page 2 and I get the next two but page three 404s!:

    /author/admin/page/1/ - works
    /author/admin/page/2/ - works
    /author/admin/page/3/ - 404s

    $wp_query->max_num_pages correctly returns 4

  29. Mark / t31os
    Moderator
    Posted 3 years ago #

    Can you tell me what theme you're using and also if you have any plugins running?

  30. lost_in_wp
    Member
    Posted 3 years ago #

    Sure - my own custom theme and no plugins.

Topic Closed

This topic has been closed to new replies.

About this Topic