Home › Forums › General Issues › Get Users with meta query filtering on foreach loop from ACF Users field
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>id ); ?>" style="text-decoration:none!important;">
<?php if ('health' == get_user_meta($user->ID, 'mepr_select_ident', true)) { ?><img />/wp-content/uploads/2023/01/D4HGN_Logo_2.png" alt="Health">
<?php } elseif ('design' == get_user_meta($user->ID, 'mepr_select_ident', true)) { ?><img />/wp-content/uploads/2023/01/D4HGN_Logo_1.png" alt="Design">
<?php } elseif ('research' == get_user_meta($user->ID, 'mepr_select_ident', true)) { ?><img />/wp-content/uploads/2023/01/D4HGN_Logo_3.png" alt="Research">
<?php } elseif ('global' == get_user_meta($user->ID, 'mepr_select_ident', true)) { ?><img />/wp-content/uploads/2023/01/D4HGN_Logo_4.png" alt="Global">
<?php } elseif ('network' == get_user_meta($user->ID, 'mepr_select_ident', true)) { ?><img />/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?
The bits inside the loop are not problematic. As they work elsewhere. It associates to a Memberpress form that has certain selections associated with logos (clients request). The issue is definitely within the foreach loop/meta query. But I can’t seem to find the solution.
this part
'meta_query' => array(
array(
'key' => 'field_63d0017ab088f',
'value' => $current_user->ID,
'compare' => '=='
)
)
should be
'meta_query' => array(
array(
'key' => 'field_63d0017ab088f', // replace this with the field name
// field key will not work
'value' => '"'.$current_user->ID.'"', // enclose ID in quotes
'compare' => 'LIKE' // change to LIKE
)
)
a user field is stored as a serialized array of IDs a strings, so you need to use “LIKE”
example: a:3:{i:0;s:1:"1";i:1;s:1:"2";i:2;s:1:"3";}
This is the serialization of
array("1", "2", "3")
searching for like 1 without the quotes would match all items and not just the first one, so you need to add quotes around your value to get just the fist one.
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.