Home › Forums › General Issues › Showing post object field values within a wp_user_query loop
Dear all,
I have extended my user profiles using ACF fields containing custom meta, custom post types and custom taxonomies. I have a category page template which returns users in a particular category. Within a wp_user_query loop I am able to show the values for certain user meta fields using ‘user_’ and the user’s ID. However I am struggling to show the values for a post object field ‘user_organisation’ which is connected to the post object ‘organisation’ which is a custom post type as shown in the code below.
<?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) { // variable must be called $post (IMPORTANT)
$people .= the_title();
$people .= the_permalink();
}
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 .= 'Sorry, we don\'t currently have any people in this category.
';
}
echo $people;
?>
Any pointers on where I am going wrong would be most appreciated.
Rowan
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!
Hi @rowanpurdy
You have two issues:
1. You’ve done a foreach loop of the post data and you even use wp_reset_postdata()
but you’ve missed the setup_postdata()
function.
2. Any function (ACF or WP Core) that is the_something()
echoes the results. This is not desired whenever you save the value to a variable.
Here’s your code (from the first snippet) modified:
<?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) { // variable must be called $post (IMPORTANT)
setup_postdata($post);
$people .= get_the_title();
$people .= get_permalink();
}
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 .= 'Sorry, we don\'t currently have any people in this category.
';
}
echo $people;
?>
Hi Jonathan,
Thank you for steering me in the right direction and apologies for the poor formatting of the second reply.
Your first point has corrected my poor syntax but now the data is not showing. I assume this related to your second point.
What code do you recommend for showing the title of the post object organisation
? The user meta field user_organisation
stores the ID of the post object organisation
and I can’t figure out how to get the post title.
Many thanks
Rowan
Jonathan,
Wow I must have had a long day yesterday and didn’t even spot that you had already answered my question. Your second point was key and alludes to my failure to understand the key difference between the_title()
and get_the_title()
. Thanks for taking the time to help me out.
For the benefit of others:
the_title()
echos out the title of the current post to the frontend.get_the_title()
however returns it as a variable, but does not echo it outHaha we all have those days!
Glad you worked it out and by now you’ll never mistake the_ and get_ again 😉
Best of luck in your project.
The topic ‘Showing post object field values within a wp_user_query loop’ 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.