如何使用多个角色参数获取WP_USER_QUERY

时间:2015-11-23 作者:Sanjay Nakate

我有两个用户角色

1) 供应商2)自由供应商

在这里,我试图通过那里的用户角色获取用户vendorfreevendor 使用以下代码,但查询仅获取freevendor.

$vendor_total_args = array (
    \'role\' => \'vendor\',
    \'role\' => \'freevendor\',
   \'orderby\'            => $orderby,
  \'order\'               => $order,
    \'meta_query\' => array(
        \'relation\' => \'AND\',
        array(
            \'key\'     => \'pv_merchant__experiance_dropdwon\',
            \'value\'   => $_POST[ \'pv_merchant__experiance_dropdwon1\' ],
            \'compare\' => \'LIKE\'
        ),
        array(
            \'key\'     => \'pv_merchant_specialization\',
            \'value\'   => $_POST[ \'pv_merchant__experiance_dropdwon2\' ],
            \'compare\' => \'LIKE\'
        )
    )
);
那么如何获取多个用户角色vendorfreevendor 使用上述代码段。

1 个回复
SO网友:phatskat

WP_User_Query 只接受一个“role”参数,并且由于数组具有相同的键两次,因此只使用第二个键:

array(
    \'role\' => \'vendor\',
    \'role\' => \'freevendor\',
);
与以下内容相同:

array(
    \'role\' => \'freevendor\',
);
您可以进行两次调用,每个角色集一次:

$user_query_args = array (
    \'role\'       => \'vendor\',
    \'orderby\'    => $orderby,
    \'order\'      => $order,
    \'meta_query\' => array(
        \'relation\' => \'AND\',
        array(
            \'key\'     => \'pv_merchant__experiance_dropdwon\',
            \'value\'   => $_POST[ \'pv_merchant__experiance_dropdwon1\' ],
            \'compare\' => \'LIKE\'
        ),
        array(
            \'key\'     => \'pv_merchant_specialization\',
            \'value\'   => $_POST[ \'pv_merchant__experiance_dropdwon2\' ],
            \'compare\' => \'LIKE\'
        )
    )
);

$vendors = new WP_User_Query( $user_query_args )->results;

$user_query_args[\'role\'] = \'freevendor\';

$freevendors = new WP_User_Query( $user_query_args )->results;
然后合并这两个结果集。或者看看注释,它显示了如何编写结合这两个角色的元查询。

相关推荐