Home › Forums › General Issues › Filter User Query
I am using the code below to display all of my users on the front end.
<?php
$args = array(
'meta_key' => 'last_name',
'orderby' => 'meta_value',
'order' => 'ASC',
'exclude' => array(1,8,9),
);
// The Query
$user_query = new WP_User_Query( $args );
// User Loop
if ( ! empty( $user_query->results ) ) {
foreach ( $user_query->results as $user ) {
um_fetch_user( $user->id );
?>
However what I want to do is to be able to also filter out users that are linked to a specific DEPARTMENT in my organization.
In ACF I have a field called “department” which is a checkbox field. Users can check multiple departments in their profile. One such example is “Social Work”.
How can I modify my query above to only show the users that have checked the box “Social Work” (among possible other values)?
THANKS!
Hi @juiceex
I believe you need to use the ‘meta_query’ option. This page should give you more idea about it: https://codex.wordpress.org/Class_Reference/WP_User_Query#Custom_Field_Parameters. You can see the example how to query posts based on custom fields here (under the “3. Multiple custom field values (array based values)” section): https://www.advancedcustomfields.com/resources/query-posts-custom-fields/#examples. Even though it is talking about posts, I believe you can do it for the users too as they have similar ‘meta_query’ option.
I hope this helps 🙂
Hi, still totally lost on this.
I have updated the query to look like this:
<?php
$args = array(
'meta_query' => array(
'relation' => 'AND',
array(
'meta_key' => 'department',
'meta_value' => 'Social Work',
'meta_compare' => '=',
),
array(
'meta_key' => 'last_name',
'orderby' => 'meta_value',
'order' => 'ASC',
'exclude' => array(1,8,9),
),
)
);
// The Query
$user_query = new WP_User_Query( $args );
// User Loop
if ( ! empty( $user_query->results ) ) {
foreach ( $user_query->results as $user ) {
um_fetch_user( $user->id );
?>
So the way I read this is that it should be looking for users that have “SOCIAL WORK” checked off in the “DEPARTMENT” ACF field that I created….(its a checkbox field).
AND, it should then look at the USER LAST NAME, and ORDER those in ASC order.
However when I do this code, I still get tons of users listed linked to all kinds of departments, not just ones that include SOCIAL WORK as I want.
Please help!
Hi @juiceex
You need to put the ‘meta_key’, ‘orderby’, ‘order’, and ‘exclude’ options outside of the ‘meta_query’ option like this:
<?php
$args = array(
'meta_key' => 'last_name',
'orderby' => 'meta_value',
'order' => 'ASC',
'exclude' => array(1,8,9),
'meta_query' => array(
'relation' => 'AND',
array(
'meta_key' => 'text',
'meta_value' => 'check1',
'meta_compare' => '=',
),
)
);
// The Query
$user_query = new WP_User_Query( $args );
// User Loop
if ( ! empty( $user_query->results ) ) {
foreach ( $user_query->results as $user ) {
print_r( $user );
}
}
?>
Please check the example on the bottom of the page I gave you before.
Hope this helps 🙂
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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.