我想在编辑帖子页面上的作者选择下拉列表中更改自定义帖子类型的用户列表。我可以使用过滤器挂钩吗?我还没有找到任何关于过滤钩子的信息,它可以满足我的需求。
钩子应该(理论上)让我返回一个用户数组,这些用户将填充底部的选择框。我想这样做的原因是,我可以根据用户的角色有条件地过滤掉不同帖子类型的用户。作为管理员(或其他管理员),我不想在将用户设为作者之前检查用户是否具有特定角色。
代码示例:
add_filter('example_filter', 'my_custom_function');
function my_custom_function ( $users ){
// Get users with role 'my_role' for post type 'my_post_type'
if( 'my_post_type' == get_post_type() ){
$users = get_users( ['role' => 'my_role'] );
}
// Get users with role 'other_role' for post type 'other_post_type'
if( 'other_post_type' == get_post_type() ){
$users = get_users( ['role' => 'other_role'] );
}
return $users;
}