1

PHP novice here... so thanks for your patience and generous answers in advance.

I'm trying to show a list of all users who match two fields with the current user, but it's not working so I would appreciate any help!

$current_user_id = get_current_user_id();
$current_team_id = get_user_meta( $current_user_id, 'seeking_first', true );
$current_team_id = get_user_meta( $current_user_id, 'team_manager', true );
$args = array(
    'meta_query' => array(
        'relation' => 'AND',
        array(
            'key' => 'person_talent',
            'value' => 'seeking_first',
            'compare' => '='
        ),
        array(
            'key' => 'team_number',
            'value' => 'current_team_id',
            'compare' => '='
        )
    )
);

$user_query = new WP_User_Query( $args );

if ( ! empty( $user_query->get_results() ) ) {
    foreach ( $user_query->get_results() as $user ) {
        echo '<li>' . $user->first_name . ' - ' . $user->user_email . '</li>';
    }
} else {
    echo 'No users found.';
}

1 Answer 1

1

You're very close.

First, make sure you're using unique variable names. I have set $current_seeking_id for the meta key seeking_first and $current_team_id for the meta key team_manager.

Next, you have to use these in your query to match. I've placed these variables at the value for your meta_query.

This assumes that the seeking_first value matches the person_talent value and the team_manager value matches the team_number value. This may not be the case and you may have to do some additional work for these to match.

$current_user_id = get_current_user_id();
$current_seeking_id = get_user_meta( $current_user_id, 'seeking_first', true );
$current_team_id = get_user_meta( $current_user_id, 'team_manager', true );
$args = array(
    'meta_query' => array(
        'relation' => 'AND',
        array(
            'key' => 'person_talent',
            'value' => $current_seeking_id,
            'compare' => '='
        ),
        array(
            'key' => 'team_number',
            'value' => $current_team_id,
            'compare' => '='
        )
    )
);

$user_query = new WP_User_Query( $args );

$results = $user_query->get_results();
if ( ! empty( $results ) ) {
    echo '<ul>';
    foreach ( $results as $user ) {
        echo '<li>' . $user->first_name . ' - ' . $user->user_email . '</li>';
    }
    echo '</ul>';
} else {
    echo 'No users found.';
}

Note: I also cleaned up the query result to set a variable for the get_results so as to only call this once. See $results = $user_query->get_results();

2
  • Thanks so much, my first Q here and can't believe that people like yourself take the time to help newbies like myself!!
    – Phil
    Commented Mar 6, 2019 at 3:45
  • Not a problem at all. I was in your position once. Just remember you can always help someone else out. Commented Mar 6, 2019 at 14:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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