Setting posts per page in query_posts

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

Read More

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

Related posts

Leave a Reply

2 comments

  1. 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' );