Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm experiencing some problems with my pagination when displaying custom post types. I would like to display 9 posts and then display a numeric pagination. This is working, some links are generated with (for me) correct URLs : http://mywebsite/tutorial/page/2 or http://mywebsite/tutorial/taxonomy/page/2 but it always finish on a 404 page.

Any idea is welcome, the following is my code so if you see any problem :)

Thanks by advance.

Cyril

<?php
    $args = array(
        'post_type' => 'tutorial',
        'paged' => ((get_query_var('paged')) ? get_query_var('paged') : 1),
        'posts_per_page' => 9
    );
    query_posts($args);
?>

[MAIN LOOP]

<?php numeric_pagination(); ?>

Here is the numeric_pagination() function (found on the web, working on another website...) :

function numeric_pagination($pages = '', $range = 2) {
global $paged;
$showitems = ($range * 2) + 1;
if(empty($paged)) $paged = 1;
if($pages == '') {
    global $wp_query;
    $pages = $wp_query->max_num_pages;
        if(!$pages) {
            $pages = 1;
        }
}   

if(1 != $pages) {
    echo '<div class="numeric-pagination"><p>';
    if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo '<a href="'.get_pagenum_link(1).'">&laquo;</a>';
    if($paged > 1 && $showitems < $pages) echo '<a href="'.get_pagenum_link($paged - 1).'">&lsaquo;</a>';

    for ($i=1; $i <= $pages; $i++) {
        if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems )) {
            echo ($paged == $i)? '<span class="current">'.$i.'</span>':'<a href="'.get_pagenum_link($i).'" class="inactive" >'.$i.'</a>';
        }
    }

    if ($paged < $pages && $showitems < $pages) echo '<a href="'.get_pagenum_link($paged + 1).'">&rsaquo;</a>';  
    if ($paged < $pages-1 &&  $paged+$range-1 < $pages && $showitems < $pages) echo '<a href="'.get_pagenum_link($pages).'">&raquo;</a>';
    echo '</p></div>';
}

}

There is maybe a problem with my custom post type and my taxonomies so I give also the code for that :

/* POST TYPE */

add_action('init','create_tutorials_post_type');
function create_tutorials_post_type() {
    // Labels
    $labels = array(
        'name' => 'Tutorials',
        'singular_name' => 'Tutorial',
        'add_new' => 'Add new',
        'add_new_item' => 'Add new tutorial',
        'edit_item' => 'Edit',
        'new_item' => 'New tutorial',
        'view_item' => 'View tutorial',
        'search_items' => 'Search tutorial',
        'not_found' =>  'No tutorial found',
        'not_found_in_trash' => 'No tutorial found in trash', 
        'parent_item_colon' => '',
        'menu_name' => 'Tutorials'
    );
    // Arguments
    $args = array(
        'labels' => $labels,
        'public' => true,
        'publicly_queryable' => true,
        'show_ui' => true, 
        'show_in_menu' => true, 
        'query_var' => true,
        'rewrite' => true,
        'capability_type' => 'post',
        'has_archive' => true, 
        'hierarchical' => false,
        'menu_position' => null,
        'supports' => array('title','editor','author','thumbnail','excerpt','comments')
    );
    // Register post type
    register_post_type('tutorial',$args);
}

/* TAXONOMY */

add_action('init','create_tutorials_taxonomies');
function create_tutorials_taxonomies() {
    // Labels
    $labels = array(
        'name' => 'Tutorial types',
        'singular_name' => 'Tutorial type',
        'search_items' => 'Search a type',
        'all_items' => 'All types',
        'parent_item' => 'Parent type',
        'parent_item_colon' => 'Parent type:',
        'edit_item' => 'Edit type', 
        'update_item' => 'Update type',
        'add_new_item' => 'Add new type',
        'new_item_name' => 'New tutorial type',
        'menu_name' => 'Types'
    );
    // Arguments
    $args =  array(
        'hierarchical' => true,
        'labels' => $labels,
        'show_ui' => true,
        'query_var' => true,
        'rewrite' => array('slug' => 'tutorials', 'hierarchical' => true)
    );
    // Register taxonomy
    register_taxonomy('tutorial_type',array('tutorial'),$args);
}
share|improve this question
add comment

4 Answers

Change predefined posts_per_page to minimum value in wordpress settings or add code below to functions.php. Wordpress using default parameter. For example: 10. And if your settings in query_posts or WP_Query look like "posts_per_page=>2" and number of posts in custom post type less then 10, after clicking to /page/2/ you will redirected to page 404.

Use this code for functions.php for solve problem:

if( !is_admin() ){  
    add_action( 'pre_get_posts',  'set_per_page'  );
}
function set_per_page( $query ) {
    global $wp_the_query;
    if($query->is_post_type_archive('tutorial')&&($query === $wp_the_query)){
    $query->set( 'posts_per_page', 1);
    }
  return $query;
}
share|improve this answer
add comment

Hey I had a similar issue.

The problem was WordPress is setup to show 10 posts per page by default which clashed with my query (limiting it to 2 posts) to fix the issue I changed the WordPress setting (Settings / Reading in the admin dashboard) to 1.

share|improve this answer
    
Thank you but I already tried this and the problem is still there :) –  Cyril Jul 12 '11 at 9:17
add comment

I'd use paginate_links() for this:

global $wp_query;

$big = 999999999; // need an unlikely integer

echo paginate_links( array(
    'base' => str_replace( $big, '%#%', get_pagenum_link( $big ) ),
    'format' => '?paged=%#%',
    'current' => max( 1, get_query_var('paged') ),
    'total' => $wp_query->max_num_pages
) );

This is the latest and greatest example from the codex article linked above. It's generally better to leverage a core function whenever the opportunity is available.

share|improve this answer
add comment

I solved this problem by using parse_query filter. For details take a look here: Fix custom query pagination without changing site-wide posts-per-page settings

share|improve this answer
    
unfortunately this is not working... but it's a good solution to fix this problem if you just need to change de site-wide posts per page setting –  Cyril Sep 25 '11 at 11:13
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.