有没有可能限制多个用户只能编辑APAST(或post),其中这些用户的自定义元密钥与自定义post的自定义分类法或元密钥相匹配?
是的,您可以使用user_has_cap
当current_user_can()
或user_can()
调用-这两个函数用于检查当前用户或特定用户是否有权限执行诸如编辑帖子、删除条款、更改网站选项等操作。
下面是一个工作示例,使用capability_type
设置为org_profile
这也是柱状段塞:
Note: 更换org_profile
使用正确的立柱类型,org_id
使用正确的元密钥organization
使用正确的分类法slug,如果用户元存储术语ID而不是slug或name,请确保将整数传递给has_term()
, e、 g。has_term( (int) $user_org_id, ... )
. (注:如果删除注释,函数实际上非常简单。)
add_filter( \'user_has_cap\', \'wpse_393410\', 10, 4 );
function wpse_393410( $allcaps, $caps, $args, $user ) {
/* With the following, and assuming the current user\'s ID is 123:
current_user_can( \'edit_post\', 456 );
user_can( 123, \'edit_post\', 456 );
$args would be array( \'edit_post\', 123, 456 ).
i.e. array( \'edit_post\', <user ID>, <post ID> )
*/
if ( ! empty( $args[2] ) && \'edit_post\' === $args[0] &&
\'org_profile\' === get_post_type( $args[2] )
) {
// If the current user does NOT have any of the roles in the include list, we
// do nothing.
$include = array( \'org_user\' );
if ( empty( array_intersect( (array) $user->roles, $include ) ) ) {
return $allcaps;
}
$user_org_id = get_user_meta( $args[1], \'org_id\', true );
// If the user meta is empty, we do nothing. Else, check if the post has a term
// with the same SLUG OR NAME in the \'organization\' taxonomy.
// Note: We just need to disable one of the caps (in $caps).
if ( $user_org_id && ! has_term( $user_org_id, \'organization\', $args[2] ) ) {
$allcaps[ $caps[0] ] = false;
}
}
return $allcaps;
}
如果要使用post meta,请替换此:
$user_org_id = get_user_meta( $args[1], \'org_id\', true );
// If the user meta is empty, we do nothing. Else, check if the post has a term
// with the same SLUG OR NAME in the \'organization\' taxonomy.
// Note: We just need to disable one of the caps (in $caps).
if ( $user_org_id && ! has_term( $user_org_id, \'organization\', $args[2] ) ) {
$allcaps[ $caps[0] ] = false;
}
使用此选项:
$user_org_id = get_user_meta( $args[1], \'org_id\', true );
$post_org_id = get_post_meta( $args[2], \'org_id\', true );
// If the post OR user meta is empty, we do nothing. Else, check if the post meta
// matches the user meta.
// Note: We just need to disable one of the caps (in $caps).
if ( $post_org_id && $user_org_id && $user_org_id !== $post_org_id ) {
$allcaps[ $caps[0] ] = false;
}
此外,在上述函数中,我使用了一个包含列表(
$include
), 但如果您愿意,可以使用排除列表,例如:。
$exclude = array( \'administrator\', \'custom\' );
然后做
! empty( array_intersect( $roles, $exclude ) )
.