Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 更改wp下拉列表用户-按用户角色筛选_Php_Wordpress - Fatal编程技术网

Php 更改wp下拉列表用户-按用户角色筛选

Php 更改wp下拉列表用户-按用户角色筛选,php,wordpress,Php,Wordpress,我尝试自定义wp\u下拉菜单\u用户功能: <form method="post" > <?php foreach ($_POST as $key => $val): ?> <?php endforeach ?> <?php wp_dropdown_users(array('selected' => ($_POST['user'] ? : get_current_user_id()))); ?> <input type=

我尝试自定义
wp\u下拉菜单\u用户
功能:

<form method="post" >
 <?php foreach ($_POST as $key => $val): ?>
 <?php endforeach ?>
 <?php wp_dropdown_users(array('selected' => ($_POST['user'] ? : get_current_user_id()))); ?>
  <input type="submit" value="go" />
 </form>

下拉字段应仅列出角色为“设备”(“角色”=>“设备”)的所有用户。 我怎样才能实现它?谁能帮帮我?
非常感谢

即使未在
wp\u下拉菜单\u users
函数中记录,也可以在args中传递“role”参数。您可以传递
WP\u User\u Query::prepare\u Query()
接受的任何参数

例如
echo wp\u下拉菜单\u用户(数组('role'=>'administrator')将显示具有管理员角色的用户下拉列表。对你来说,你应该

<?php echo wp_dropdown_users(
    array(
        'selected' => ($_POST['user'] ? :    get_current_user_id()),
        'role' => 'device'
    )
); ?>


这假设您有角色为“设备”的用户,这不是默认的。别忘了回音
wp\u下拉菜单\u用户
在没有它的情况下不会显示任何内容。

接受的答案可能在某一点上有效,但似乎不再有效。Wordpress在到达查询之前过滤掉“role”参数。由于以下原因,我对其进行了一些修改:

$users_with_role = implode(",", get_users ( array ( 'fields' => 'id', 'role' => 'customer' ) ) );

wp_dropdown_users(array(
    'selected' => ($_POST['user'] ? :    get_current_user_id()),
    'include_selected' => true,
    'include' => $users_with_role
));
创建筛选器:

function filter_wp_dropdown_users_args($query_args) {
    $query_args['role'] = 'device';
    return $query_args;
}
并使用它:

add_filter('wp_dropdown_users_args', 'filter_wp_dropdown_users_args');
wp_dropdown_users();
remove_filter('wp_dropdown_users_args', 'filter_wp_dropdown_users_args');

非常感谢,詹姆斯。这对我有帮助!不适用于我(WP 4.5)-wordpres过滤器我相信这仍然有效,但如果有人发现它不再有效,我会编辑它,因为它现在已经很旧了。我认为我不理解被接受的答案是如何工作的,因为这实际上没有一个被接受的答案。