Home › Forums › General Issues › Pull in fields from page with same name as username
Hi, using the example of pulling in custom field data from other pages using $other_page, I’m trying to pull data in from a page which is called the same name as the current logged in user. So far, I have:
global $current_user;
get_currentuserinfo();
$other_page = is_page( '$current_user->user_login' );
echo the_field('favourite_movie', $other_page);
…but while in theory it should work, it doesn’t. I have my repeater all set up now if could get this page name/username working, all my issues will be sorted (well, for tonight at least).
Any advice would be most appreciated.
Phil
Hi @phil.owen
I think your code is caugin $other_page to be a true|false.
$other_page = is_page( '$current_user->user_login' );
Looks like that is a boolean returning function mate, perhaps you meant to write this?
$other_page = $current_user->user_login
if( is_page( $other_page ) )
{
the_field('favourite_movie', $other_page);
}
2 issues with your code:
1. quote marks in the is_page function
2. echo the_field does not need an echo
Hi Elliot,
Thanks for you help with this. I’ve tried your suggestion which broke my page – assuming it was because $other_page = $current_user->user_login was missing a ; on the end?
Anyhow, this is what I have so far but still doesn’t pull anything in from the ACF field in the page:
<?php
global $current_user;
get_currentuserinfo();
$other_page = $current_user->user_login;
if( is_page( $other_page ) ) {
the_field('favourite_movie', $other_page);
}
?>
Try this, probably won’t work but who knows… 🙂
<?php function get_currentuserinfo() {
global $current_user;
$other_page = $current_user->user_login;
$field = get_field('favourite_movie', $other_page);
if( is_page( $other_page ) ) {
echo $field;
}
}
?>
If not, should post the whole code since you mentioned using a repeater.
Hi Nuro, that’s a no on that but thank you for your suggestion.
It’s crazy. It seems so easy yet just doesn’t work.
Trying it normally with manual ID of page like:
<?php
$other_page = 890;
?>
<?php the_field('field_name', $other_page); ?>
…works great so the data IS being passed from the other page. I then had another idea of trying this way:
<?php
global $current_user;
get_currentuserinfo();
$username = $current_user->user_login;
$other_page = $username;
?>
<p><?php the_field('favourite_movie', $other_page); ?></p>
…which didn’t work either.
The username of the current user IS also working when I ask for it conventionally:
<?php global $current_user;
get_currentuserinfo();
echo 'Username is ' . $current_user->user_login . "\n";
?>
Baffled.
Elliot, how can I debug this? I’ve tried dumping the values:
<?php
global $current_user;
get_currentuserinfo();
$other_page = $current_user->user_login;
$values = get_field('favourite_movie');
var_dump($values);
?>
and get bool(false) printed out?
Hi @phil.owen
Your above ‘debugging’ code is missing the $post_id param. Also, you are not testing that the user_login is correct. Try this:
$other_page = $current_user->user_login;
var_dump($other_page);
$values = get_field('favourite_movie', $other_page);
var_dump($values);
It returns this:
string(10) “CompanyOne” bool(false)
CompanyOne is the username of the current logged in user.
Hi @phil.owen
Aah, I see the problem now. The $post_id param is wrong.
You need to create a string like this "user_{$user_id}"
;
So perhaps $other_page
should be 'user_' . $current_user->ID
?
Good luck
Thanks
E
Hi Elliot,
I’ve tried implementing like this:
<?php
global $current_user;
get_currentuserinfo();
$other_page = 'user_' . $current_user->ID;
?>
<p><?php echo the_field('favourite_movie', $other_page); ?></p>
Is this what you mean? It doesn’t seem to do anything.
Thanks for you help with this.
P
An Woocommerce ‘My Account’ file. Sits in mytheme/woocommerce/.
Like I say, it works fine when I have the ID of the page in there so it can communicate fine with the global data. Just cannot get username as $other_page.
Hi @phil.owen
Just to clarify, you don’t want the username as $other_page, you want the user ID:
$other_page = 'user_' . $current_user->ID;
Have you debugged the value returned yet?
var_dump($values);
Perhaps the value is loading, but it can’t be echo’ed due to it’s variable type?
This:
$other_page = 'user_' . $current_user->ID;
var_dump($other_page);
$values = get_field('favourite_movie', $other_page);
var_dump($values);
Gave me this:
string(6) "user_3" bool(false)
Hi @phil.owen
So you can correctly get the $post_id value, that’s good. The next step is to understand why you are getting false
back from the get_field function.
In what file are you running the get_field function? Are you aware that if you use it in your functions.php file, the get_field will not work as expected until after the init action?
Perhaps this is the issue?
Thanks
E
Hi. I am having the same issue. I use an ACF Field group to extend an user profile. acf_form works perfect, and I have seen the fields in the wp_usermeta correctly for my user_id. But, when I get the get_field(‘phone_number’,’user_’.$current_user->ID) it returns false.
Sorry. Forget it. It has worked for me. I was making a mistake when doing get_field. Sorry for the inconveniences
The topic ‘Pull in fields from page with same name as username’ 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!
Are you building WordPress sites with ACF and @BeaverBuilder, and wanted to use your ACF Blocks in both the block editor and Beaver Builder?
— Advanced Custom Fields (@wp_acf) May 10, 2023
The BB team recently added support for using ACF Blocks in Beaver Builder. Check it out 👇https://t.co/UalEIa5aQi
© 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.