Exciting News! Flipper Code is now WePlugins! Same commitment to excellence, brand new identity.

How to use edit_post_type_per_page filter in WordPress

Sandeep Kumar Mishra
Sandeep Kumar Mishra
June 8, 2023
5 minutes read

Have you ever wanted to tweak how many posts show up on your WordPress site for a specific post type? If yes, then the edit_post_type_per_page filter is the hero you need! This hook provides you the flexibility to change the number of posts displayed per page based on the post type. Whether you’re customizing a theme or building a plugin, this hook is a handy tool in your WordPress developer toolkit.

Live Examples

Example 1: Basic Usage

In this example, we demonstrate how to use the edit_post_type_per_page filter to modify the number of posts per page. The function modify_edit_post_type_per_page_defaults is hooked to the filter and can be customized as per your needs.

    function weplugins_modify_edit_post_type_per_page_defaults($posts_per_page) { 
        // Update the $posts_per_page variable according to your website requirements and return this variable.
        return $posts_per_page; 
    }
    // add the filter
    add_filter( "edit_post_type_per_page", "weplugins_modify_edit_post_type_per_page_defaults", 10, 1 );
    

Example 2: Conditional Modification

Sometimes, you might want to change the number of posts per page only for a specific post type. Here’s how you can do it conditionally.

    function weplugins_modify_edit_post_type_per_page_conditionally($posts_per_page) { 
        global $post_type;
        if ($post_type == 'custom_post_type') {
            $posts_per_page = 30; // Set to a different value for custom post type
        }
        return $posts_per_page; 
    }
    add_filter( "edit_post_type_per_page", "weplugins_modify_edit_post_type_per_page_conditionally", 10, 1 );
    

Example 3: Removing a Hook

If you need to remove a hook callback, you can use the remove_filter function. Ensure you use the same callback function name, priority, and the number of arguments while removing the hook.

    remove_filter( "edit_post_type_per_page", "weplugins_modify_edit_post_type_per_page_defaults", 10, 1 );
    

Access Premium WordPress Plugins

Contact Us

If you’re looking for customizations or facing any issues with this hook, feel free to reach out to us. Visit our Contact Us page, and we’d be more than happy to assist you!

Sandeep Kumar Mishra

Sandeep Kumar Mishra

Sandeep Kumar Mishra writes about WordPress and Artificial Intelligence, offering tips and guides to help you master your website and stay updated with the latest tech trends.

Explore the latest in WordPress

Trying to stay on top of it all? Get the best tools, resources and inspiration sent to your inbox every Wednesday.