这可以通过以下两种方式实现:
具有pre_get_posts
小的修改将需要它,就像实际的元值名称一样
function alter_edit_php_query_remove_posts($query) {
$screen = get_current_screen();
if(is_admin() && $screen->post_type == \'post\') {
$current_user = wp_get_current_user();
$key = \'nickname\';
$single = true;
$user_meta_value = get_user_meta( $current_user->ID, $key, $single);
$meta_query = array(
array(
\'key\'=>\'_edit_last\',
\'value\'=>$user_meta_value,
\'compare\'=>\'=\',
),
);
$query->set(\'meta_query\',$meta_query);
}
}
add_action(\'pre_get_posts\',\'alter_edit_php_query_remove_posts\');
使用
map_meta_cap
:
add_filter(\'map_meta_cap\', \'prevent_user_view_post\', 3, 4 );
function prevent_user_view_post( $required_caps, $cap, $user_id, $args ){
if( ( $cap==\'read_post\'|| $cap=\'edit_post\') ){
$post_id = $args[0]??0;
$post_meta_value=get_post_meta($post_id,\'meta_key\',true);
$key = \'nickname\';
$single = true;
$user_meta_value = get_user_meta( $user_id, $key, $single );
$user_meta_value = 2;
if ( $post_meta_value != $user_meta_value && $post_id ==1 ){
$required_caps[] = \'do_not_allow\';
}
}
return $required_caps;
}