Home › Forums › General Issues › Filtering user_query by custom fields (checkboxes)
i have a page called “germany” where i created this loop to filter the users by a custom field meta value:
<?php
$user_query = new WP_User_Query(
array(
'fields' => ID',
'meta_key' => 'country',
'meta_value' =>
"germany",
'compare' => 'IN' ) ); $user_ids = $user_query->get_results();
?>
that works.
but now i need a little more advanced one because some users work in china AND russia. so on the user pages i have ACF checkboxes like “russia, usa, china, germany …” and now i want to show people on the page “germany” who have checked the checkbox “germany” (even if they checked “chiona, too”). so in the template for the page “germany” i need a query who checks IF ONE of the meta_values of the meta_key “country” is “germany” (so that i can show the users who work in germany). i hope someone understands this 😀
how to even get the data of a checkbox field with a wp_user_query? i tried
<?php echo implode(', ', get_field('country', $user_id )); ?>
as mentioned in http://www.advancedcustomfields.com/resources/field-types/checkbox/ but it shows me nothing :-/ in a normal query it works.
Okay i get the field this way:
<?php echo implode(', ', get_field('country', 'user_'. $user_id )); ?>
but how do i filter the loop with this field?
Think i need an example like here:
http://www.advancedcustomfields.com/resources/how-to/how-to-query-posts-filtered-by-custom-field-values/
but with a “checkbox” field.
got it (it is explained here: http://www.advancedcustomfields.com/resources/how-to/how-to-query-posts-filtered-by-custom-field-values/ and here is the user_query explained: http://codex.wordpress.org/Class_Reference/WP_User_Query):
<?php
// args
$args = array(
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'country',
'value' => 'germany',
'compare' => 'LIKE'
)
)
);
// The Query
$user_query = new WP_User_Query( $args );
// User Loop
if ( ! empty( $user_query->results ) ) {
foreach ( $user_query->results as $user ) {
echo '<p>' . $user->display_name . '</p>';
}
} else {
echo 'No users found.';
}
?>
<?php
// aaaaaand reset ...
wp_reset_query();
?>
The topic ‘Filtering user_query by custom fields (checkboxes)’ is closed to new replies.
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.