1

Bear with me here..

The default posts per page in the admin area is 10. During some testing I wanted to change the posts per page for my custom post archive to 2 (in WP 3.1).

The problem is that I only have 4 posts, so there should be 2 pages with 2 posts on each, but because the default is at 10, going to /page/2 returns error-404 (assuming because with 10 posts per page there wouldn't be a second page)

The only way to get around this was to set the default in the admin area to 1, but it's not really ideal as i now have to do a custom query_post for all post type archives to set the posts per page.

Does anyone have a better way to do this, or any ideas? Thanks.

archive-project.php:

<?php get_header(); ?>

    <?php
        global $wp_query;
        query_posts(array_merge($wp_query->query, array(
            'paged'          => get_query_var('paged'),
            'posts_per_page' => 2
        )));
    ?>

    <h1 class="title"><?php _e('Previous work', 'fullycharged'); ?></h1>

    <?php if (have_posts()): while(have_posts()): the_post();?>
        <a href="<?php the_permalink(); ?>" id="post-<?php the_ID(); ?>" <?php post_class('launch col col-' . $i); ?>>
            <span class="project-title"><?php the_title(); ?></span>
            <?php the_content(); ?>
        </a>
    <?php endwhile; endif; ?>

    <?php if ($wp_query->max_num_pages > 1): ?>
        <div id="nav-below" class="navigation">
            <div class="nav-previous"><?php next_posts_link( __( '<span class="meta-nav">&larr;</span> Older posts', 'twentyten' ) ); ?></div>
            <div class="nav-next"><?php previous_posts_link( __( 'Newer posts <span class="meta-nav">&rarr;</span>', 'twentyten' ) ); ?></div>
        </div>
    <?php endif; ?>

<?php get_footer(); ?>

register post type:

register_post_type('project', array(
    'capability_type' => 'post',
    'has_archive' => true,
    'hierarchical' => false,
    'labels' => array(
        'name' => __('Projects', 'fullycharged'),
        'singular_name' => __('Project', 'fullycharged'),
        'all_items' => __('All Projects', 'fullycharged'),
        'add_new_item' => __('Add New Project', 'fullycharged'),
        'edit_item' => __('Edit Project', 'fullycharged'),
        'update_item' => __('Update Project', 'fullycharged')
    ),
    'menu_icon' => get_stylesheet_directory_uri() . '/images/monitor-off.png',
    'menu_position' => 5,
    'public' => true,
    'publicly_queryable' => true,
    'exclude_from_search' => false,
    'rewrite'  => array('slug' => 'work', 'with_front' => false),
    'supports' => array('title', 'editor', 'thumbnail', 'custom-fields')
));
7
  • Can i see the code you're using? Are you calling get_query_var( 'page' );?
    – designpx
    Feb 8, 2011 at 16:40
  • @Designpx global $paged; query_posts(array_merge($wp_query->query, array( ` 'paged' => $paged,` ` 'posts_per_page' => 2` ))); Thats all the changes to the query i've made, no get_query_var( 'page' );, but i've used the $paged global because archive pagination uses $paged rather than $page Sorry for the bad formatting Feb 8, 2011 at 17:14
  • @Andre Lawson, Do you have a regular page and a custom post type with the same slug? For example say you have a page called "Recipes" and it's slug is "/recipes" and then you have a post type called "Recipes" and the rewrite rule on is set to 'rewrite' => 'recipes' ? If so you're confusing wordpress with your links Feb 8, 2011 at 18:26
  • No regular page at at /recipes , just registered the custom post in the functions.php file. I'm able to get the first page of my recipes archive at /recipes, but when I change the posts_per_page in the archive-recipes.php file to something other than the value in WP-Admin -> Settings -> Reading will result in a "page 'n' shouldnt exist (when it really should, according to my altered posts_per_page)" error. I dont think its my code in archive-recipes.php because placing exit; at the top of the file doesn't do anything Feb 8, 2011 at 18:41
  • @eileen @designpx So in other words, the request to /recipes shows the first page of the archive, /recipes/page/2 doesn't reach the archive-recipes.php template file because page 2 shouldn't exist, based on the settings in WP-Admin -> Settings -> Reading Feb 8, 2011 at 19:07

2 Answers 2

2

Here's what I usually use the pre_get_posts action to change a single query value for a taxonomy or category page:

/**
 * Control the number of search results
 */
function custom_posts_per_page( $query ) {
    if ( $query->is_tax('mytaxonomy') || $query->is_category('mycategory') ) {
        set_query_var('posts_per_page', 9);
    }
}
add_action( 'pre_get_posts', 'custom_posts_per_page' );
3
  • The docs here codex.wordpress.org/Function_Reference/query_posts give the impression that you can just "update" one query variable using the query_posts() function. That didn't work for me, but your example using $wp_query->query_vars does. Any idea why?
    – Niko Nyman
    Sep 8, 2011 at 8:15
  • By adding onto the existing query vars we aren't rewriting anything. This is like saying: "Give me your settings, I'm going to change one value, then reset them." before running the query. Jul 31, 2012 at 18:51
  • I'm just wondering why query_posts() doesn't seem to work that way to allow changing one value, even though the docs say so?
    – Niko Nyman
    Sep 24, 2012 at 10:08
1

There's a discussion on Wordpress.org support forums about this exact issue. The OP on that discussion has come up with an answer but is yet to post it.

http://wordpress.org/support/topic/error-404-on-pagination-when-changing-posts_per_page-on-query_posts

Thanks for your help anyway.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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