0

I asked a question a few days ago (link) about a problem I encountered with WP 4.1 and a WP_User_Query I ran with a custom meta array. My current code worked until I upgraded to 4.1:

    if( is_array($user->role) == true){ 
    $staff_role=esc_attr(implode(", ",$user->role));
    } else {$staff_role=$user->role;};

    $pm = array(
    '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'
                            )
                    )
    );
// User Queries
    $program_manager = new WP_User_Query( $pm );
// User Counts
    $pm_count = count($program_manager->results);
// User Loop
    if ( ! empty( $program_manager->results ) ) {
        echo '<h2>Program Managers (' . $pm_count . ')</h2>';
        foreach ( $program_manager->results as $user ) {
            echo '<li>',
            pods_user_profile_display($user);
            echo '</li><div class="clearboth"></div><br />';
    }
    echo '<hr />';
    } else {
    echo '<h2>Program Managers</h2>No managers found.';
    }

However, I didn't get an answer.

Hopefully this new question will get an answer:

I want to display a list of WP users with the role "Administrator" or "Program Manager" in a directory on my website. For example, I have 1 administrator and 12 program_managers. I want the list to display all 13 users sorted by display_name

0

Since the roles for a user are stored in (dbprefix)_capabilities, I'd try grabbing the database prefix and then using it to grab people with the right capabilities (caps are in a serialized array):

global $wpdb;
$prefix = $wpdb->prefix;
$meta_name = "{$prefix}capabilities";

'meta_query' => array(
                'relation' => 'OR',
                    array(
                        'key' => "{$meta_name}"
                        'value' => 'program_manager',
                        'compare' => 'LIKE'
                        ),
                    array(
                        'key' => "{$meta_name}",
                        'value' => 'administrator',
                        'compare' => 'LIKE'
                        )
                )
);

The above should give you all users that are program managers or administrators.

The thing I see that may have broken your code is as follows:

if( is_array($user->role) == true){ 
    # This will break your meta query as it could create "subscriber, administrator" as a key
    # and your meta query does 'key' => $staff_role
    $staff_role=esc_attr(implode(", ",$user->role));
} else {
    $staff_role=$user->role;
}

The second a user gets a second role, the query starts looking for a key value that contains comma separated roles. Of course, maybe that is what you seek, but I'd double check in the database as such a key would be a bit odd.

  • Thank you @Privateer. In order to reach my desired outcome, I converted the meta_query back into a variable and added the 'order_by'=>'display_name' before feeding it into the WP_User_Query – Sean Jan 15 '15 at 20:06

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.