Home › Forums › General Issues › show array of relationship field values in USER query
i try to show the relationship values from a custom field that is displayed on user pages, but
1: when the user has chosen some relationships i get only the ID of the field and
2: when the user has not chosen any field i get a error (“Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs …”)
this is my code:
<?php
$values = $user->user_faecher;
if (isset($values)) {
foreach($values as $value) {
if($value != end($values)){
echo $value;
} else{
echo $value;
}
}
}
?>
Hi @herrfischer
I don’t understand your code..
What is $user->user_faecher
? That does not exist in WordPress user object per default.
With this code the error is gone:
<?php
$values = $user->user_faecher;
if (!empty($values)) {
foreach($values as $value) {
if($value != end($values)){
echo $value;
} else {
echo $value;
}
}
}
?>
but i have still only the IDs … i need the names π .
Yes but I need to know what in the world user_faecher is?
That value should not exist so $values should just be nothing for you..
Please post your entire code either here or in a pastebin so I can have a look.
hello,
here is a pastebin of the page template: http://pastebin.com/20BeMiVc
this page has a user_query and “user_faecher” is a ACF relationship field that lists all wordpress pages on the user pages. the user choses some pages and the title of the pages should appear in the query on each user listing.
field option:
user page:
frontend – the result are the IDs of the chosen fields, but i need the names (in this case “Biologie” + “Physik”):
hello jonathan,
i found out i have to echo the title, it’s that easy *gg
<?php
$values = $user->user_faecher;
if (!empty($values)) {
foreach($values as $value) {
if($value != end($values)){
echo get_the_title( $value );
} else {
echo get_the_title( $value );
}
}
}
?>
thanks anyway for your effort!
Hi,
Hm okay.. but if $user is a regular wp user object in WP then $values should not work. To retrieve a user meta field in ACF you need to do
$values = get_field('user_faecher', 'user_'.$user->ID);
Also in your code, this part does absolutely nothing:
$args = array (
'meta_query' => array(
array(
),
),
);
You’re basically passing no arguments into wp_user_query. Which is fine if you’re good with the default arguments but if so you should remove that code along with the $args inside WP_User_Query.
thanks jonathan, i need the args for later … . something like:
$args = array (
'order' => 'ASC',
'orderby' => $sortit,
'meta_key' => 'hide_user',
'meta_value' => 'visible'
);
Alright π
You know your code best of course. Best of luck in your project!
You must be logged in to reply to this topic.
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!
Accordions are a great way to group related information while allowing users to interactively show and hide content. In this video, Damon Cook goes in-depth on how to create an accessible accordion block using ACF PROβs Repeater field.https://t.co/RXT0g25akN
— Advanced Custom Fields (@wp_acf) March 2, 2023
© 2023 Advanced Custom Fields.
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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.