How to Change Admin Pagination on Posts and Comments Pages in WordPress

Home » Snippets » How to Change Admin Pagination on Posts and Comments Pages in WordPress
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

Pagination is essential for organizing large amounts of content in a way that’s easy to navigate. However, the default pagination settings in WordPress for post and comments pages may not be sufficient for your needs. In this article, we’ll explore how to change the admin pagination settings for post and comment pages, so you can customize it to fit your preferences and improve the user experience.

					function WPTurbo_change_pagination( $query ) {
    // Check if we are on admin panel and on the Edit Posts or Edit Comments page
    if ( is_admin() && ( $query->is_main_query() ) && ( $query->get( 'post_type' ) == 'post' || $query->get( 'post_type' ) == 'page' || $query->get( 'comment_type' ) == 'comment') ) {
        $query->set( 'posts_per_page', '10' ); // Set the number of items per page
    }
}
add_action( 'pre_get_posts', 'WPTurbo_change_pagination' );
				

In this code snippet, we are changing the number of items per page for posts pages and comments within the WordPress admin panel. The function is called WPTurbo_change_pagination() and it takes the $query variable as an argument.

First, the code checks if we are on the WordPress admin panel with is_admin(). Next, it checks if we are on the Edit Posts or Edit Comments page with the $query->get('post_type') and $query->get('comment_type') conditions.

If these conditions are met, the code will set the number of items per page to 10 with $query->set( 'posts_per_page', '10' );. This means that 10 items (posts or comments) will be displayed per page instead of the default number.

Finally, we hook the WPTurbo_change_pagination() function into the pre_get_posts action using add_action(). This ensures that the function is called whenever the main query is being executed, allowing us to modify the query parameters before the query is executed.

Overall, this code snippet provides an easy way to modify the pagination settings for posts and comments within the WordPress admin panel.

Register an account to save your snippets or go Pro to get more features.