WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am designing a theme where the home page (i.e. is_home()) should only display the latest blog post. I want to do something like $wp_query->update_option('posts_per_page', 1) every time I am on the home page. All other pages that display posts (like archives) should just follow the user defined option.

This seems a bit unnecessary to do every single time since the option should just be set once, right? Is it possible within The Loop to just ignore the user-set option of posts_per_page and force have_posts() to just be set to one post?

In general, where should this kind of "set-it-once" stuff go? I don't really think it should be a plug-in because it is theme specific. I also don't want to mess with the user's options which is why update_option isn't the best choice for this problem.

share|improve this question
up vote 1 down vote accepted

Alternate approach (if you want/need to keep this in functions.php and go via hooks instead of modifying template) would be something like this:

add_action( 'pre_get_posts', 'pre_get_posts_home' );

function pre_get_posts_home( $query ) {

    global $wp_query;

    if( $wp_query == $query && $query->is_home )
        $query->set( 'posts_per_page', 1 );
share|improve this answer
Where can I find a canonical list of filter/action hooks? (adambrown.info/p/wp_hooks) doesn't list pre_get_posts, nor does the Codex mention it on (codex.wordpress.org/Plugin_API/Filter_Reference). – Avery Chan Jun 4 '11 at 0:39
I did a little more research and noticed that on the Codex, pre_get_posts (I thought it was a filter so I looked for it as a filter) is identified as an action. Is there a difference in the above code between calling add_filter vs add_action? The difference in semantics on the Codex isn't quite clear to me. – Avery Chan Jun 4 '11 at 0:50
@Avry ah, sorry, should be add_action() indeed :) The difference is that filter expects and uses returned value, action does not. But technically add_filter() and add_action() are identical (in fact one of those just calls another) and distinction is for human convenience, not code. Edited snippet. – Rarst Jun 4 '11 at 9:22

posts_per_page should be set once the at settings reading panel, and acts as a general option unless defined else using query_posts() for example if you want your homepage to display only one then the place this code above your loop:

if (is_home()){ // conditional check for homepage
    global $query_string;
    parse_str( $query_string, $args );
    $args['posts_per_page'] = 1;
    query_posts( $args );
share|improve this answer
I think you meant $args['posts_per_page'] = 1; ? :) – Rarst Jun 3 '11 at 18:36
Yep, my bad, fixed it, thanks – Bainternet Jun 3 '11 at 19:29
Where should this code snippet go? In loop.php? – Avery Chan Jun 3 '11 at 23:33
like i posted in my answer, just above the loop – Bainternet Jun 3 '11 at 23:40

I prefer use a function than use global variable, like this:

if (is_home()) set_query_var('posts_per_page', 1);
share|improve this answer

Your Answer


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.