2016-05-16 119 views
0

我想在编辑帖子页面的作者选择下拉列表中更改自定义帖子类型的用户列表。有没有我可以使用的过滤器钩子?我一直无法找到任何我想要的滤波器钩子上的信息。WordPress在编辑帖子中更改作者列表的过滤器作者框

挂钩应该(理论上)让我返回一个用户数组,这些将是填充底部选择框的用户。我想这样做的原因是我可以有条件地筛选出不同职位类型的用户。作为管理员(或其他管理员),我不想在将用户作为作者之前检查用户是否具有某种角色。的代码

实施例:

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; 
} 
+0

我们可以看到你的代码吗? – surajsn

+1

不知道为什么这个问题得到了downvoted ...更多的问题与一些更多的澄清和细节以及一些示例代码。我现在没有任何代码在我的主题中,但是因为我没有过滤器来连接。 –

回答

0

可以使用钩 'wp_dropdown_users_args'。

在主题的functions.php文件中添加以下代码片段。

add_filter('wp_dropdown_users_args', 'change_user_dropdown', 10, 2); 

function change_user_dropdown($query_args, $r){ 
// get screen object 
$screen = get_current_screen(); 

// list users whose role is e.g. 'Editor' for 'post' post type 
if($screen->post_type == 'post'): 
    $query_args['role'] = array('Editor'); 

    // unset default role 
    unset($query_args['who']); 
endif; 

// list users whose role is e.g. 'Administrator' for 'page' post type 
if($screen->post_type == 'page'): 
    $query_args['role'] = array('Administrator'); 

    // unset default role 
    unset($query_args['who']); 
endif; 

return $query_args; 
} 

让我知道这是否适用于您。

相关问题