Home › Forums › General Issues › Showing post object field values within a wp_user_query loop › Reply To: Showing post object field values within a wp_user_query loop
Quick update – the code above is actually outputting the data for the title and link for the organisation that the user is connected to (from the the_title() and the_permalink() calls) however the data appears before the
I changed the code as follows by adding <span> and tags around the_title() and the_permalink() calls but the data remains output before the
<?php
$args = array(
'posts_per_page' => -1,
'cat' => $queryinfo->term_id
);
$user_query = new WP_User_Query( $args );
$users = $user_query->get_results();
if (!empty($users)) {
$people .= '<ul>';
foreach ($users as $user) { // loop through each user
$user_info = get_userdata($user->ID); // get all the user's data
$people .= '<li><a href="#"><span class="title">' . $user_info->first_name . ' ' . $user_info->last_name . '</span></a>';
if( !empty($user_info->description) ) {
$people .= '<span>' . $user_info->description . '</span>';
}
if( get_field('user_jobtitle', 'user_' .$user_info->ID) ) {
$people .= '<span>' . get_field('user_jobtitle', 'user_' .$user_info->ID) . '</span>';
}
$posts = get_field('user_organisation', 'user_' .$user_info->ID);
// Note that field 'user_organisation' is connected to the post object 'organisation' which is a custom post type
if( $posts ) {
foreach( $posts as $post) { // IMPORTANT variable must be called $post
$people .= '<span><a href="' . the_permalink() . '">' . the_title() . '</a></span>';
}
wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly
}
$people .= '</li>';
} // end for each loop
$people .= '</ul>';
} // end initial if statement
else{
$people .= '<p>Sorry, we don\'t currently have any people in this category.</p>';
}
echo $people;
?>
Much head scratching at my end!
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.