1

I just updated to WP 4.1 and a User_Query I was using is now returning 0 instead of the expected results.

$pm = array(
    //'role' => 'program_manager',
    'orderby' => 'display_name',
    'meta_query' => array(
        'relation' => 'OR',
        array(
            'key' => $staff_role,
            'value' => 'program_manager',
            'compare' => 'LIKE'
        ),
        array(
            'key' => $staff_role,
            'value' => 'administrator',
            'compare' => 'LIKE'
        )
    )
);

This function was inserted into a new WP_User_Query() to return all users who have a custom role of Program Manager or Administrator which was then subsequently displayed on a page. If I remove the meta_query and simply query the program_manager role then it works fine, but I still need to include the administrator in this count. What has changed?

  • Is the commenting out of the role key/value pair intentional? – jdm2112 Jan 12 '15 at 19:48
  • Yes, fully intentional. I simply left that there from an earlier version of the variable. I could essentially remove that entirely but kept it for a reminder to myself. For additional clarification, I use the variable here in the following WP_User_Query : $program_manager = new WP_User_Query( $pm ); and run the following loop to display the results: foreach ( $program_manager->results as $user ) – Sean Jan 12 '15 at 21:01
  • Have you turned on error logs / debugging to make sure there's no syntax changes or helpful warnings? – Howdy_McGee Jan 12 '15 at 22:07
  • Also, could you expand on what $staff_role is and how it's set, maybe that is being overwritten somewhere so that it returns unexpected results. – Howdy_McGee Jan 12 '15 at 22:14
  • There's nothing in the debug log that indicates a potential problem related to this (that I can see anyway) $staff_role is defined below: if( is_array($user->role) == true){ $staff_role=esc_attr(implode(", ",$user->role)); } else {$staff_role=$user->role;}; – Sean Jan 12 '15 at 22:46
1

From the Codex: Multiple custom user fields handling

$args = array(
'meta_query' => array(
    'relation' => 'OR',
    0 => array(
        'key'     => 'country',
        'value'   => 'Israel',
        'compare' => '='
    ),
    1 => array(
        'key'     => 'age',
        'value'   => array( 20, 30 ),
        'type'    => 'numeric',
        'compare' => 'BETWEEN'
    )
)
);

Try adding the 0 => array and 1 => array

  • This makes no difference in the output. So as you can see in the $staff_role, there are some users who have multiple roles, thus the reason I am using the LIKE comparison – Sean Jan 13 '15 at 14:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.