$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Showing all users who match 2 meta fields with current user|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Showing all users who match 2 meta fields with current user

matteradmin9PV0评论

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.';
}

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.';
}
Share Improve this question asked Mar 5, 2019 at 0:31 PhilPhil 132 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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();

Post a comment

comment list (0)

  1. No comments so far