skip to Main Content

I’m hoping that someone can help me.

I’m trying to work out why my loop isn’t working.

I’m wanting to loop through all users on the site and then use a meta query to display only the users that have the current users name in a user field. Essentially like if someone added you as a friend/connection, but you didn’t add them. So the the current user can see who’s added them to their connections.

Does that make sense?

I’m using an ACF user field to do this.

The code i’m using that isn’t working is below:

<div class="connectedtoyou usersloop teamloop" style="margin-top:40px!important;margin-bottom:40px!important;display:none!important;">
<hr>
<div class="users">
<h3 class="center centered">Users connected to you</h3>
<?php 
$current_user = wp_get_current_user();
//$connect_user = 'user_'.$current_user->ID;
//$connect_field = get_field('user_follows');

$args = array(
        //'role' => 'subscriber',  
        'orderby' => 'display_name',
        'order'   => 'ASC',
        'meta_query' => array(
            array(
                'key' => 'field_63d0017ab088f',
                'value' => $current_user->ID,
                'compare' => '=='
            )
         )
        );
$users = get_users($args);
if( $users ): ?>
<div class="row isotope">
    <?php foreach( $users as $user ): ?>
    <div class="col fade-up item">
            <div class="item-container"><a href="<?php echo get_author_posts_url( $user->id ); ?>" style="text-decoration:none!important;">
                <?php if ('health' == get_user_meta($user->ID, 'mepr_select_ident', true)) { ?><img src="<?php echo get_home_url(); ?>/wp-content/uploads/2023/01/D4HGN_Logo_2.png" alt="Health">
                <?php } elseif ('design' == get_user_meta($user->ID, 'mepr_select_ident', true)) { ?><img src="<?php echo get_home_url(); ?>/wp-content/uploads/2023/01/D4HGN_Logo_1.png" alt="Design">
                <?php } elseif ('research' == get_user_meta($user->ID, 'mepr_select_ident', true)) { ?><img src="<?php echo get_home_url(); ?>/wp-content/uploads/2023/01/D4HGN_Logo_3.png" alt="Research">
                <?php } elseif ('global' == get_user_meta($user->ID, 'mepr_select_ident', true)) { ?><img src="<?php echo get_home_url(); ?>/wp-content/uploads/2023/01/D4HGN_Logo_4.png" alt="Global">
                <?php } elseif ('network' == get_user_meta($user->ID, 'mepr_select_ident', true)) { ?><img src="<?php echo get_home_url(); ?>/wp-content/uploads/2023/01/D4HGN_Logo_5.png" alt="Network">
                <?php } else { ?>
                <?php } ?>
                <h4 class="darkgrey"><?php echo esc_html( $user->first_name ); ?> <?php echo esc_html( $user->last_name ); ?></h4>
            </a></div>
            </div>
    <?php endforeach; ?>
</div>
<?php else: ?>
<p>No users are connected to you. <a href="https://d4hgn.com/network/network-users/">Click here to see who's on the network</a>.</p>
<?php endif; wp_reset_postdata(); ?>
</div>
</div>

Can anyone help me work this out?

2

Answers


  1. Chosen as BEST ANSWER

    The issue was this:

    'value' => '"'.$current_user->ID.'"'

    Which I hadn't thought of.

    Thanks for all your help with this :)


  2. try this....
    $args = array(
            //'role' => 'subscriber',  
            'orderby' => 'display_name',
            'order'   => 'ASC',
            'meta_query' => array(
                array(
                    'key' => 'user_follows',
                    'value' => $current_user->ID,
                    'compare' => '='
                )`enter code here`
             )
            );
        $users = get_users($args);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search