Using WordPress ‘edit_posts_per_page’ PHP filter

The edit_posts_per_page WordPress PHP filter lets you modify the number of posts displayed per page when listing “posts”.

Usage

add_filter('edit_posts_per_page', 'your_custom_function_name', 10, 2);

function your_custom_function_name($posts_per_page, $post_type) {
    // your custom code here
    return $posts_per_page;
}

Parameters

  • $posts_per_page (int): Number of posts to be displayed. Default is 20.
  • $post_type (string): The post type.

More information

See WordPress Developer Resources: edit_posts_per_page

Examples

Change Posts Per Page for a Specific Post Type

Modify the number of posts per page for a custom post type called “products”.

add_filter('edit_posts_per_page', 'change_products_posts_per_page', 10, 2);

function change_products_posts_per_page($posts_per_page, $post_type) {
    if ($post_type === 'products') {
        $posts_per_page = 30;
    }
    return $posts_per_page;
}

Set Different Posts Per Page for Various Post Types

Change the number of posts per page for different post types.

add_filter('edit_posts_per_page', 'change_multiple_post_types_per_page', 10, 2);

function change_multiple_post_types_per_page($posts_per_page, $post_type) {
    switch ($post_type) {
        case 'products':
            $posts_per_page = 30;
            break;
        case 'testimonials':
            $posts_per_page = 10;
            break;
    }
    return $posts_per_page;
}

Change Posts Per Page for All Post Types

Set the same number of posts per page for all post types.

add_filter('edit_posts_per_page', 'change_all_post_types_per_page', 10, 2);

function change_all_post_types_per_page($posts_per_page, $post_type) {
    $posts_per_page = 25;
    return $posts_per_page;
}

Display All Posts on One Page

Show all posts on a single page for a specific post type called “events”.

add_filter('edit_posts_per_page', 'display_all_events_on_one_page', 10, 2);

function display_all_events_on_one_page($posts_per_page, $post_type) {
    if ($post_type === 'events') {
        $posts_per_page = -1;
    }
    return $posts_per_page;
}

Display Posts Per Page Based on User Role

Change the number of posts per page for the “projects” post type, based on the user’s role.

add_filter('edit_posts_per_page', 'change_posts_per_page_based_on_role', 10, 2);

function change_posts_per_page_based_on_role($posts_per_page, $post_type) {
    if ($post_type === 'projects') {
        $user = wp_get_current_user();
        if (in_array('editor', (array) $user->roles)) {
            $posts_per_page = 15;
        } elseif (in_array('administrator', (array) $user->roles)) {
            $posts_per_page = 30;
        }
    }
    return $posts_per_page;
}