1

I want my blog page to display 5 posts at most. But I have many custom queries (wp_query) on my site, some of them embedded together: how to make Settings > Reading > Blog pages show at most setting NOT affect my custom queries?

2 Answers 2

2

If you're creating your own custom WP_Query queries, you can use the posts_per_page parameter:

$args = array( 
    // ...
    'posts_per_page' => $some_number,
    //...
);
$my_query = new WP_Query( $args );

If you want all the posts, use -1 as your $some_number.

Reference

3

The preferred method would be to hook the pre_get_posts() action.

function set_home_pagesize( $query )
{
    if ( is_admin() || !$query->is_main_query() )
        return;

    if ( is_home() )
    {
        $query->set( 'posts_per_page', 5 );
        return;
    }

}
add_action( 'pre_get_posts', 'set_home_pagesize', 1 );

This is assuming that your blog page is your home. You may need to use different conditionals if it is not.

Your Answer

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

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