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?
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();
?>