Skip to main content
Commonmark migration
Source Link

This question is a bit old, but for those finding this in the modern era, you should never call query_posts. From the Wordpress codex:

query_posts() is overly simplistic and problematic way to modify main query of a page by replacing it with new instance of the query. It is inefficient (re-runs SQL queries) and will outright fail in some circumstances (especially often when dealing with posts pagination).

 

...

 

TL;DR don't use query_posts() ever;

Instead, you should use the pre_get_posts hook in functions.php as follows:

function hwl_home_pagesize( $query ) {
    // Behave normally for secondary queries
    if ( is_admin() || ! $query->is_main_query() )
        return;

    if ( is_home() ) {
        // Display only 1 post for the home page
        $query->set( 'posts_per_page', 1 );
        return;
    }

    // Otherwise, use whatever is set in the Wordpress Admin screen
    $query->set( 'posts_per_page', get_option('posts_per_page'); );
}
add_action( 'pre_get_posts', 'hwl_home_pagesize', 1 );

However, beware that in some cases, (such as adjusting post offsets), using a pre_get_posts hook can mangle your pagination. Fixing this isn't super-hard, but it's something to be aware of. There's an example of how to fix this here.

This question is a bit old, but for those finding this in the modern era, you should never call query_posts. From the Wordpress codex:

query_posts() is overly simplistic and problematic way to modify main query of a page by replacing it with new instance of the query. It is inefficient (re-runs SQL queries) and will outright fail in some circumstances (especially often when dealing with posts pagination).

 

...

 

TL;DR don't use query_posts() ever;

Instead, you should use the pre_get_posts hook in functions.php as follows:

function hwl_home_pagesize( $query ) {
    // Behave normally for secondary queries
    if ( is_admin() || ! $query->is_main_query() )
        return;

    if ( is_home() ) {
        // Display only 1 post for the home page
        $query->set( 'posts_per_page', 1 );
        return;
    }

    // Otherwise, use whatever is set in the Wordpress Admin screen
    $query->set( 'posts_per_page', get_option('posts_per_page'); );
}
add_action( 'pre_get_posts', 'hwl_home_pagesize', 1 );

However, beware that in some cases, (such as adjusting post offsets), using a pre_get_posts hook can mangle your pagination. Fixing this isn't super-hard, but it's something to be aware of. There's an example of how to fix this here.

This question is a bit old, but for those finding this in the modern era, you should never call query_posts. From the Wordpress codex:

query_posts() is overly simplistic and problematic way to modify main query of a page by replacing it with new instance of the query. It is inefficient (re-runs SQL queries) and will outright fail in some circumstances (especially often when dealing with posts pagination).

...

TL;DR don't use query_posts() ever;

Instead, you should use the pre_get_posts hook in functions.php as follows:

function hwl_home_pagesize( $query ) {
    // Behave normally for secondary queries
    if ( is_admin() || ! $query->is_main_query() )
        return;

    if ( is_home() ) {
        // Display only 1 post for the home page
        $query->set( 'posts_per_page', 1 );
        return;
    }

    // Otherwise, use whatever is set in the Wordpress Admin screen
    $query->set( 'posts_per_page', get_option('posts_per_page'); );
}
add_action( 'pre_get_posts', 'hwl_home_pagesize', 1 );

However, beware that in some cases, (such as adjusting post offsets), using a pre_get_posts hook can mangle your pagination. Fixing this isn't super-hard, but it's something to be aware of. There's an example of how to fix this here.

Source Link
lfalin
  • 101
  • 1

This question is a bit old, but for those finding this in the modern era, you should never call query_posts. From the Wordpress codex:

query_posts() is overly simplistic and problematic way to modify main query of a page by replacing it with new instance of the query. It is inefficient (re-runs SQL queries) and will outright fail in some circumstances (especially often when dealing with posts pagination).

...

TL;DR don't use query_posts() ever;

Instead, you should use the pre_get_posts hook in functions.php as follows:

function hwl_home_pagesize( $query ) {
    // Behave normally for secondary queries
    if ( is_admin() || ! $query->is_main_query() )
        return;

    if ( is_home() ) {
        // Display only 1 post for the home page
        $query->set( 'posts_per_page', 1 );
        return;
    }

    // Otherwise, use whatever is set in the Wordpress Admin screen
    $query->set( 'posts_per_page', get_option('posts_per_page'); );
}
add_action( 'pre_get_posts', 'hwl_home_pagesize', 1 );

However, beware that in some cases, (such as adjusting post offsets), using a pre_get_posts hook can mangle your pagination. Fixing this isn't super-hard, but it's something to be aware of. There's an example of how to fix this here.