0

Ok, so this sounds a little complex, but in theory should be easy.

What I'm trying to achieve:

ACF data is pulled from a WP page which has the same name as the current logged in user.

Basically I will have a WP page set up for each company. I will create a login for each company with an appropriate username. The name of the company page will be the exact same name as the username I create. It will have Advanced Custom Fields entered with data.

I want to dynamically pull in the ACF data from the company page based on the current user logged in into my page template.

So if company A are logged in, I want them to view the custom fields from company A's page. Company B log in and view the custom fields from the company B page.

So far, I have this:

 <?php global $user_login; get_currentuserinfo(); $username = $current_user->user_login; $other_page = $username; ?> <p><?php the_field('my_custom_field', $other_page); ?></p> 

Which in theory should work, but it just doesn't.

I can check the current username of the logged in user and echo it which returns the correct username just fine on the page.

I can also add a page ID by adding the page ID like this: $other_page = 890; and it pulls in the custom field data from the correct company page (890 being one of the company pages).

So everything works as it should independently, just not when I want to use the logged in username as the name of the page (namely $other_page).

I'm baffled with this so any help would be greatly appreciated.

1 Answer 1

0

According with docs, when you want to retrieve field attached to post using get_field / the_field you should pass a numeric ID as 2nd argument.

You are passing a string.

So your code should work if:

$login = wp_get_current_user()->user_login; $page = $login ? get_page_by_path( $login ) : false; $field = $page ? get_field('my_custom_field', $page->ID) : false; 
2
  • Hi @PhilOwen, it worked for you? Commented Sep 24, 2013 at 22:32
  • No. I couldn't get it working with this. Commented Sep 25, 2013 at 19:01

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.