Home › Forums › General Issues › Get label from User Custom Field
Hey all,
I’m working on a front-end portion of the site where I need to display the field label and data. I’m successfully able to get the field and data, however, the label seems to be blank.
Here is what I’m doing:
First, I get the fields:
$user_meta = get_fields('user_'.$userid);
Then, I loop through each to get the ones I want. I’ve prefixed the ones I want with user_q_:
$questions = array();
foreach ($user_meta as $meta_name => $value):
if(strpos($meta_name, 'user_q_') !== FALSE):
$questions[$meta_name] = $value;
endif;
endforeach;
So far so good, however, then I try to display the question labels and value and the label is blank:
<?php foreach($questions as $question => $value):
$field = get_field_object($question, false, array('load_value' => false));
?>
<h3><?php echo $field['label']; ?></h3>
<div><?php echo $value; ?></div>
<?php endforeach; ?>
If I print out the $field object from above, here is the results for one of the values:
Array ( [key] => field_user_q_about [label] => [name] => user_q_about [type] => text [order_no] => 1 [instructions] => [required] => 0 [id] => acf-field-user_q_about [class] => text [conditional_logic] => Array ( [status] => 0 [allorany] => all [rules] => 0 ) [default_value] => [formatting] => html )
As you can see, the label value is blank.
Any thoughts, ideas, hints, things to look at? Am I doing something completely wrong?
Thanks for any advice!
Try using get_field_objects() including the (user) ID, as in:
$user_meta = get_field_objects('user_' . $userID);
foreach($user_meta as $meta) :
if (strpos($meta['name'], 'user_q_') !== FALSE) { ?>
<h3><?php echo $meta['label']; ?></h3>
<div><?php echo $meta['value']; ?></div>
<?php }
endforeach;
Notice the field key is returning the field’s name prefixed with “field_” – this means ACF can’t find the actual field object for the post (but it can find the meta_value).
@wells5609 Awesome! You lead me to try something that got me to the answer.
I can’t do exactly what you said cause I needed to do the get_fields call for some other data. But, your answer prompted me to try:
$field = get_field_object($question, 'user_'.$userid, array('load_value' => false));
Essentially adding in the user_ID# instead of passing in FALSE. This did the trick for me. Not sure why I didn’t try that before!
Thanks!
The topic ‘Get label from User Custom Field’ 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.