Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
267 views
in Technique[技术] by (71.8m points)

php - 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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 );

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...