Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

Owais Akber
PLUS
Owais Akber
Courses Plus Student 1,501 Points

Custom field for user.

I have prepared a custom posts as 'tasks' and add a custom field for user with multiple selection. Now I want to retrieve user from that custom field and used the following code.

$the_user = get_field('user');
foreach($the_user as $user => $user_value){
    echo "user : ".$user. ", value :".$user_value;
    echo "<br>";  
}

EDIT: please press edit and see what i did for formating

Result for this query is very unexpected as it is echo the following line. user :0, value :Array

This should be noted that this code i'm using on single-tasks template and all other fields values are retrieved without WP_Query object.

I am unable to understand what is wrong here....

Owais Akber
Owais Akber
Courses Plus Student 1,501 Points

ok, I got the solution myself :). get_field('user') function returns back an array of multi dimension. First dimension is indexed whereas second dimension is Associative.

I used this syntax to get my required value.

$user = $the_user[0]['ID']

This return me user ID.

Leaving this comment if anyone else need help in this regard.