admin管理员组

文章数量:1201413

I want to change the default screen option value for posts per page in the wp-admin area when listing posts in pending mode. The default value is set to 20.

Changing the value directly in the Screen Options tab will only affect the user, not all users like this answer suggests.

Any workable solution?

I want to change the default screen option value for posts per page in the wp-admin area when listing posts in pending mode. The default value is set to 20.

Changing the value directly in the Screen Options tab will only affect the user, not all users like this answer suggests.

Any workable solution?

Share Improve this question edited May 23, 2017 at 12:40 CommunityBot 1 asked Apr 18, 2014 at 17:06 Christine CooperChristine Cooper 8,8877 gold badges60 silver badges93 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 3

Just an addiction to @KrzysiekDróżdż answer.

When viewing a specific post status the url query string variable 'post_status', is set to the name of the status, so you can use $_GET['post_status'] to narrow the effect of @KrzysiekDróżdż code only for pending posts:

function my_edit_per_page( $result, $option, $user ) {
  $status = filter_input( INPUT_GET, 'post_status', FILTER_SANITIZE_STRING );
  if ( $status === 'pending' && (int) $result < 1 )
  return 20; // or whatever you want
}
add_filter( 'get_user_option_edit_post_per_page', 'my_edit_per_page', 10, 3 );  // for posts

It won't be very hard to do. Just add this to your functions.php or into your plugin:

function my_edit_per_page( $result, $option, $user ) {
    if ( (int)$result < 1 )
        return 20; // or whatever you want
}
add_filter( 'get_user_option_edit_page_per_page', 'my_edit_per_page', 10, 3 );  // for pages
add_filter( 'get_user_option_edit_post_per_page', 'my_edit_per_page', 10, 3 );  // for posts

This way you will change the default value. So if user changes it, it will remember user's choice.

The only problem is that this setting is common for entire post type editor. I'm afraid it won't be so easy to set it depending on post status (and not breaking anything).

I really don't understand why the choice to compare to < 1 from the other answers. Also, if $result was zero (and it can't be), the function won't return anything.

I'm using it like this to force a minimum of 50 items per page in an array of pages. Add @gmazzap $status code if needed.

$perpages = ['flamingo_inbound_messages', 'wpcf7_contact_forms', 'plugins', 'edit_post', 'edit_page'];

foreach($perpages as $option) {
    add_filter( "get_user_option_{$option}_per_page", function ( $result, $option, $user ) {
        if ( (int)$result < 50 ) $result = 50;
        return $result;
    }, 9999, 3 );
}

Sometimes, it's not obvious nor easy to find the page name used in the option. I've looked for them directly in the database. Just apply some number on a page and look for it as the meta_value in wp_usermeta:


本文标签: listChange default screen option value for posts per page