I am trying to display users attached to a Custom Post Type. I created an ACF-Users-Field which returns the ID. This field is displayed at my Custom Post Type. Now I would like to display the names of the users selected in this field.
I am able to display all names of my users (if I remove the part ‘meta_query’ of my arguments, but if i add the meta_query array to my arguments nothing is displayed. I am not sure, maybe the mistake is the ‘value’, but I have no idea what to change there. Below I show the relevant code:
<article>
<?php
$args = array(
'role' => 'Subscriber',
);
$my_user_query = new WP_User_Query( $args );
$editors = $my_user_query->get_results();
if ( ! empty( $editors ) ) {
foreach ( $editors as $editor ) {
$editor_info = get_userdata( $editor->ID );
$args = array(
'post_type' => 'projekt',
'meta_query' => array(
array(
'key' => 'projektteilnehmer', // name of custom field - return ID
'value' => $post->ID ,
'compare' => 'LIKE'
)
)
);
// The User Query
$user_query = new WP_User_Query( $args );
// The User Loop
if ( ! empty( $user_query->results ) ) { ?>
<?php foreach ( $user_query->results as $user ) {
echo $user->user_firstname;;
}
} else {
echo 'nothing found';
}
} // endforeach
} else {
echo __( 'Kein Mitglied gefunden.' );
}
?>
</article>
Any help appreciated
2
Answers
The answer of Jasper B pushed me in the right direction. Thank you very much:) It is working with this query:
By what your describing this code should do what you want. given that the participant’s are returned as userID
or if they are returned as user objects