WordPress.org

Ready to get started?Download WordPress

Forums

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

  1. Mark / t31os
    Moderator
    Posted 3 years ago #

    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?

  2. lost_in_wp
    Member
    Posted 3 years ago #

    Sorry Mark I didn't see that replies had gone to two pages! I thought you hadn't replied.

    No, it's not based on the twenty ten theme but I do use the get_template_part function quite a bit - but only once in Authors.php, for the loop. I still get the problem though even if I put the loop directly in the page (i.e. remove the get_template_part function).

    And yes I still have the same problem when using default permalinks i.e.

    ?author=1 is fine
    ?author=1&paged=2 is fine
    ?author=1&paged=3 404s

    Thank you for the help!

  3. Mark / t31os
    Moderator
    Posted 3 years ago #

    Testing shows setting query parameters inside the loop template works perfectly fine..

    My test code:

    if( is_author() ) {
    	$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
    	query_posts( array( 'post_type' => 'book', 'posts_per_page' => 1, 'paged' => $paged ) );
    }

    Placed near the top of loop.php (though this could essentially exist in a loop-author.php if i had one).

    Additional: Decided to test code in the author.php template aswell, my test code again below..

    // This code was placed just before the get_header line
    $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
    query_posts( array( 'post_type' => 'book', 'posts_per_page' => 1,'paged' => $paged ) );

    Results were exactly the same.. (working as it should do)..

    If your author template has a couple of lines like this...

    if ( have_posts() )
    		the_post();

    And a rewind_posts further down, your query_posts call must go before both of them..

    Hope that helps...

  4. lost_in_wp
    Member
    Posted 3 years ago #

    Sorry Mark I've only just been able to get back to this.

    I think the fix for this is going to be something similar to your fix for dreamnoir because the problem is the same but with archive pages instead. I can replicate the problem with a clean install and with the TwentyTen theme:

    1. Start with a clean install of wordpress 3.0.1

    2. At the top of functions.php (in TwentyTen theme) add:

    // Register the book post type and custom taxonomy
    add_action( 'init', 'create_book_types', 0 );
    
    function create_book_types() {
    
    	$labels = array(
    		'name' => 'Books',
    		'singular_name' => 'Book',
    		'add_new' => 'Add New',
    		'add_new_item' => 'Add New Book',
    		'edit_item' => 'Edit Book',
    		'new_item' => 'New Book',
    		'view_item' => 'View Book',
    		'search_items' => 'Search Books',
    		'not_found' => 'No books found',
    		'not_found_in_trash' => 'No books found in Trash',
    		'parent_item_colon' => ''
    	);
    
    	$args = array(
    		'labels' => $labels,
    		'public' => true,
    		'publicly_queryable' => true,
    		'show_ui' => true,
    		'query_var' => true,
    		'rewrite' => array('slug' => 'books'),
    		'capability_type' => 'post',
    		'hierarchical' => false,
    		'menu_position' => null,
    		'supports' => array('title', 'editor', 'author', 'custom-fields', 'revisions'),
    	);
    	register_post_type( 'book', $args );
    
    	register_taxonomy('bookcats', 'book' , array( 'label' => 'Book Categories', 'show_ui' => true, 'hierarchical' => true, 'rewrite' => array( 'slug' => 'book-category' ), 'query_var' => true ));
    
    }

    3. At the top of author.php add:

    $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
    
     query_posts( array( 'post_type' => 'book', 'paged' => $paged, 'posts_per_page' => 1 ) );

    4. Go to the WP admin site, log in as admin.

    5. Change permalinks to 'Day and name'.

    6. Add three 'Books' (with or without a custom category, doesn't seem to matter).

    7. View the homepage and click 'admin' under the first post (so that you can view all posts by admin).

    8. The first page should show correctly, you should be seeing the last of the three books and the pagination link for 'Older posts' should be visible BUT if you click on 'Older posts' to get to the next page I get a 'Page not found' i.e. /author/admin/page/2/ fails.

    Your fix for dreamnoir was to change the slug from plural to singular 'book' but that doesn't work here because the term isn't in the url - any ideas?

    Thanks again.

  5. Mark / t31os
    Moderator
    Posted 3 years ago #

    I have a clean (aside from my own code) install of 3.0.1.. so i took this code and plonked it in author.php (TwentyTen theme - unmodified)

    $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
    
    query_posts( array( 'post_type' => 'book', 'paged' => $paged, 'posts_per_page' => 1 ) );

    Right after the get_header() call, and it works fine for me..

    I have a book post type registered already, so i didn't need that step.

    Also switched to the Day and Name permalink structure, again author page worked correctly across all pages (i only have four entries, so only four pages, but it works).

    Maybe there's something in the post type registration that makes a difference, so here's a copy of mine for comparison..

    // Ignore this line - Forum bug work-around
    	register_post_type( 'book', array(
    		'labels' => array(
    			'name' => 'Books',
    			'singular_name' => 'Book',
    			'add_new' => 'Add New',
    			'add_new_item' => 'Add New Book',
    			'edit_item' => 'Edit book',
    			'edit' => 'Edit',
    			'new_item' => 'New Book',
    			'view_item' => 'View Book',
    			'search_items' => 'Search Books',
    			'not_found' => 'No books found',
    			'not_found_in_trash' => 'No books found in Trash',
    			'view' => 'View Book'
    		),
    		'public' => true,
    		'capability_type' => 'post',
    		'hierarchical' => false,
    		'rewrite' => array( 'slug' => 'book', 'with_front' => false ),
    		'query_var' => true,
    		'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'custom-fields', 'comments' ),
    		'taxonomies' => array( 'category', 'post_tag' )
    	) );

    My code of course sits inside a function that's hooked onto init just like yours above.

  6. wiziology
    Member
    Posted 3 years ago #

    Hey Mark,

    I am hoping you can help me as well. I have posted my problem (which is similar to the ones above) here: http://wordpress.org/support/topic/pagnation-on-author-page?replies=1#post-1790388

    If you would be so kind as to take a look and see if you can offer any insight, I would greatly appreciate it.

    Thanks in advance!

  7. soroaga
    Member
    Posted 3 years ago #

    The answer here...

    Problem is that the url vars do not contain the custom post types. For lost_in_wp the solution would be:

    ?author=1&paged=2&post_type=book

    In general:

    function author_request($qv) {
    	if(isset($qs['author_name'])){
    		$qs['post_type'] = get_post_types($args = array(
    			'public'   => true,
    			'_builtin' => false
    		));
    		array_push($qs['post_type'],'post');
    	}
    	return $qs;
    }
    add_filter('request', 'author_request');

    You can use this for feeds too:

    function myfeed_request($qv) {
    	if (isset($qv['feed']))
    		$qv['post_type'] = get_post_types();
    	return $qv;
    }
    add_filter('request', 'myfeed_request');
  8. soroaga
    Member
    Posted 3 years ago #

  9. microkid
    Member
    Posted 3 years ago #

    Thanks soroaga for sharing this solution. Note that there is a typo in the author_request function; the $qv parameter should be $qs

    function author_request($qs) {
    	if(isset($qs['author_name'])){
    		$qs['post_type'] = get_post_types($args = array(
    			'public'   => true,
    			'_builtin' => false
    		));
    		array_push($qs['post_type'],'post');
    	}
    	return $qs;
    }
    add_filter('request', 'author_request');
  10. sabahat
    Member
    Posted 3 years ago #

    Hey, I'm not sure if anyone has found this out, but I changed the slug on the Pages I created for my custom-post-type (originally 'art', and changed to 'art-ivy') and that did the trick. It didn't matter what code I used, or the permalink structure.

    I found the answer here: http://www.position-relative.com/2010/09/wordpress-custom-post-types-and-paging-problems/

    Hope this helps!

Topic Closed

This topic has been closed to new replies.

About this Topic