Last updated on October 14, 2023

Change the Number of Posts Displayed per Page

Don’t know where to add this snippet? Read our guide: How to add code snippets.

Adjust posts per page for display.

To change the number of posts displayed per page in WordPress, you can use the pre_get_posts action hook along with the posts_per_page parameter. This code snippet allows you to modify the number of posts displayed on any archive page, such as the blog page, category pages, tag pages, etc.

function wpsnippets_change_posts_per_page( $query ) {
    if ( $query->is_main_query() && ! is_admin() && $query->is_archive() ) {
        $query->set( 'posts_per_page', 10 ); // Change the number to your desired value
    }
}
add_action( 'pre_get_posts', 'wpsnippets_change_posts_per_page' );

This code snippet checks if the current query is the main query, not in the admin area, and is an archive page. If all conditions are met, it sets the posts_per_page parameter to the desired number of posts per page (in this example, 10).

You can modify the value 10 to any number you want. For example, if you want to display 5 posts per page, change it to 5.

By using this code snippet, you can easily customize the number of posts displayed on archive pages without modifying any template files or using plugins.

Examples

Example 1: Change the number of posts displayed per page in the main query

This use case demonstrates how to change the number of posts displayed per page in the main query on the front-end of a WordPress site. By default, WordPress displays 10 posts per page, but you can easily modify this value to suit your needs.

function wpsnippets_change_posts_per_page( $query ) {
    if ( $query->is_main_query() && ! is_admin() ) {
        $query->set( 'posts_per_page', 5 );
    }
}
add_action( 'pre_get_posts', 'wpsnippets_change_posts_per_page' );

In this code example, we use the pre_get_posts action hook to modify the main query before it is executed. The wpsnippets_change_posts_per_page function checks if the query is the main query and not in the admin area. If both conditions are met, it sets the posts_per_page parameter to 5.

Example 2: Change the number of posts displayed per page in a custom query

This use case demonstrates how to change the number of posts displayed per page in a custom query. This is useful when you want to create a custom loop or display a different number of posts on a specific page or template.

$args = array(
    'post_type'      => 'post',
    'posts_per_page' => 8,
);

$custom_query = new WP_Query( $args );

if ( $custom_query->have_posts() ) {
    while ( $custom_query->have_posts() ) {
        $custom_query->the_post();
        // Display post content
    }
    wp_reset_postdata();
}

In this code example, we create a custom query using the WP_Query class. We set the posts_per_page parameter to 8 to display 8 posts per page. Inside the loop, we can then access and display the post content as needed. Finally, we use wp_reset_postdata() to restore the global post data.

Example 3: Change the number of posts displayed per page for a specific post type

This use case demonstrates how to change the number of posts displayed per page for a specific post type. By default, WordPress uses the same value for all post types, but you can easily override it for a specific post type.

function wpsnippets_change_posts_per_page( $query ) {
    if ( $query->is_main_query() && ! is_admin() && $query->is_post_type_archive( 'portfolio' ) ) {
        $query->set( 'posts_per_page', 12 );
    }
}
add_action( 'pre_get_posts', 'wpsnippets_change_posts_per_page' );

In this code example, we use the pre_get_posts action hook to modify the main query before it is executed. The wpsnippets_change_posts_per_page function checks if the query is the main query, not in the admin area, and for the post type archive of “portfolio”. If all conditions are met, it sets the posts_per_page parameter to 12.

Last updated on October 14, 2023. Originally posted on October 14, 2023.

Leave a Reply

Your email address will not be published. Required fields are marked *