phpwordpresswoocommercehook-woocommercehook-wordpress

Set a default value for "number of items per page" in WooCommerce admin product list


WordPress allows each user to set his own value for "Number of items per page" in the Admin Products Page /wp-admin/edit.php?post_type=product.

I'm looking for a code to set the same value for "Number of items per page" for all users regardless of your user rol.

Is there a way to do this? Something like:

add_action('admin_init', 'set_value_for_all_users');
function set_value_for_all_users() {
    // set $Number_of_items_per_page for all users = 20
    $Number_of_items_per_page = 20;
}

enter image description here


Solution

  • In the wp_user_meta table there is a meta key edit_product_per_page that sets this limit. By default 20.

    However, to edit this setting for the admin product list you can use a filter hook from the get_items_per_page() WordPress function

    WP_List_Table::get_items_per_page() - Gets the number of items to display on a single page.

    protected function get_items_per_page( $option, $default = 20 ) {
        $per_page = (int) get_user_option( $option );
        if ( empty( $per_page ) || $per_page < 1 ) {
            $per_page = $default;
        }
     
        /**
         * Filters the number of items to be displayed on each page of the list table.
         *
         * The dynamic hook name, `$option`, refers to the `per_page` option depending
         * on the type of list table in use. Possible filter names include:
         *
         *  - `edit_{$post_type}_per_page`
         *
         * @since 2.9.0
         *
         * @param int $per_page Number of items to be displayed. Default 20.
         */
        return (int) apply_filters( "{$option}", $per_page );
    }
    

    So to answer your question, change {$post_type} with product and then you get:

    function filter_edit_product_per_page ( $per_page ) {
        // The number of items to be displayed on product page of the list table
        $per_page = 50;
        
        return $per_page;
    }
    add_filter( 'edit_product_per_page', 'filter_edit_product_per_page', 10, 1 );