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 - 16 through 30 (of 39 total)
  • You’re welcome.. 🙂

    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.

    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

    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.

    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.

    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!

    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?

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

    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.

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

    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 );

    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

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

    Sure – my own custom theme and no plugins.

    Is your theme based on the TwentyTen theme by chance? The theme uses a different approach for queries, and i’ve noticed there are some problems using query_posts alongside get_template_part based template files (as seen in the TwentyTen theme).

    Additional question, any problems with these pages when using default permalinks, ie. off?

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