使用map_meta_cap
筛选以有条件地阻止无法manage_options
(即管理员)编辑/删除某些帖子:
function wpse_188368_map_meta_cap( $caps, $cap, $user_ID, $args ) {
if ( in_array( $cap, array( \'delete_post\', \'edit_post\' ) ) && $args && ! current_user_can( \'manage_options\' ) /** Only proceed for non-administrators */ ) {
$post_id = $args[0];
if ( in_array( $post_id, array( 1, 2, 3 /* ID\'s of locked posts */ ) ) )
$caps[] = \'not_allowed\'; // Add a required capability they won\'t have - current_user_can() will then subsequently return false
}
return $caps;
}
add_filter( \'map_meta_cap\', \'wpse_188368_map_meta_cap\', 10, 4 );