您可以使用has_password
的参数WP_Query
.
下面是一个示例,您可以在edit.php
帖子类型的屏幕:
/**
* Hide password protected posts, for non-admins, in the case of \'edit-post\' screen id
*
* @link http://wordpress.stackexchange.com/a/200426/26350
*/
add_action( \'pre_get_posts\', function( \\WP_Query $q )
{
$screen = get_current_screen();
if( is_admin()
&& ! current_user_can( \'manage_options\' )
&& is_a( $screen, \'\\WP_Screen\' )
&& \'edit-post\' === $screen->id
)
$q->set( \'has_password\', false );
} );
希望您可以根据自己的需要进行调整。
附言:有一个get_current_screen_id()
函数,类似于get_current_user_id()
函数;-)
更新
/**
* Hide password protected posts, for non-admins, in the case of \'edit-post\' screen id.
* Here we don\'t restrict this to the current user.
*
* @link http://wordpress.stackexchange.com/a/200426/26350
*/
is_admin() && add_action( \'posts_where\', function( $where, \\WP_Query $q )
{
$screen = get_current_screen();
if(
! current_user_can( \'manage_options\' )
&& is_a( $screen, \'\\WP_Screen\' )
&& \'edit-post\' === $screen->id
){
global $wpdb;
$uid = get_current_user_id();
$where .= $wpdb->prepare(
" AND ( ( {$wpdb->posts}.post_password = \'\' AND {$wpdb->posts}.post_author != %d )
OR ( {$wpdb->posts}.post_author = %d ) ) ",
$uid,
$uid
);
}
return $where;
}, 10, 2 );
如果WP_Query
将支持field_query
:
如果
WP_Query
将支持
field_query
输入参数,以便更轻松地使用post字段。
下面是一个示例:
add_action( \'pre_get_posts\', function( \\WP_Query $q )
{
$screen = get_current_screen();
if( is_admin()
&& ! current_user_can( \'manage_options\' )
&& is_a( $screen, \'\\WP_Screen\' )
&& \'edit-post\' === $screen->id
)
$q->set( \'field_query\',
[
\'outer_relation\' => \'AND\',
\'relation\' => \'OR\',
[
\'field\' => \'author\',
\'value\' => get_current_user_id(),
\'compare\' => \'=\',
],
[
\'relation\' => \'AND\',
[
\'field\' => \'author\',
\'value\' => get_current_user_id(),
\'compare\' => \'!=\',
],
[
\'field\' => \'password\',
\'value\' => \'\',
\'compare\' => \'=\',
],
]
]
);
} );
我有这样一个插件的草稿,所以当它达到alpha阶段时,也许我会把它发布在这里;-)