需要帮助wp_user_query不能正常工作

时间:2016-02-17 21:16:43

标签: php sql wordpress

我需要一些帮助才能让wp_user_query工作;一直在努力并尝试很多东西但却无法正常工作。

这是我的代码:

$args = array(
    'meta_query' => array(
        'role'     => 'personal-injury-lawyer',
        'orderby'  => 'meta_value',
        'meta_key' => 'lawyer_numeric_rank',
        'order'    => 'ASC',
        array(
            'key'     => 'lawyer_location',
            'value'   => 'London',
            'compare' => '='
        ),
    )
);

$london_user_query = new WP_User_Query($args);  

if ( ! empty($london_user_query->results)) {
    foreach ( $london_user_query->results as $user ) {

我知道foreach循环没有关闭;试着缩短这个......

我正在尝试对此查询做三件事:

  1. 显示personal-injury-lawyer
  2. 的角色
  3. 显示meta_key =伦敦
  4. lawyer_location字段
  5. meta_value订购,其中meta_key = lawyer_numeric_rank按ASC顺序
  6. 我已尝试过多种方法,但无法通过伦敦位置过滤和按排名排序......

    现在代码会对位置进行过滤;但是orderby部分不起作用;如果我从中删除位置过滤器;那么orderby确实有用......

    我希望有人可以帮忙解决这个问题。

1 个答案:

答案 0 :(得分:1)

您错误地形成了元查询参数。以下代码也可以帮助您处理更多的元查询。

$args = array(
    'role'=> 'personal-injury-lawyer', //assuming you have created a role that corresponds..
    'order' => 'ASC',
    'orderby' => 'meta_value',
    'meta_key' => 'lawyer_numeric_rank',  
    'meta_query' => array(
      // 'relation'=> 'AND', --> if you want more conditions 
        array(
            'key'     => 'lawyer_location',
            'value'   => 'London',
            'compare' => '='
        ),

       /* even more conditions with OR, cumulative effect is match lawyer location AND 1 of the nested arrays


            array(
                        'relation' => 'OR',
                        array(
                                'key' => '',
                                'value' => '',
                                'compare' => '=',
                        ),
                        array(
                                'key' => '',
                                'value' => '',
                                'compare' => '=',
                        ),
            ),


       */

    )
);