Retrieve an ACF User Field and Add it to the Post Template

July 25, 2021
Retrieve an ACF User Field and Add it to the Post Template

You can use Advanced Custom Fields, ACF, to add fields to your users in the WordPress admin area, which is great! However, Oxygen's ACF integration can't yet retrieve data from those fields. This tutorial will cover retrieving an ACF field for the current post author and adding it to Oxygen dynamically. This tutorial assumes that you will already have assigned an ACF field group to your users.

First off, you will need to install your favourite scripts plugin, such as Advanced Scripts (paid) or Code Snippets (free). I personally use Advanced Scripts as I think the interface is fantastic, but prior to that, I'd used Code Snippets for a number of years.

Once you've installed the scripts plugin of your choice, you can create a new snippet with the following code:

function my_get_user_field($field) {
    $author_id = get_the_author_meta( 'ID' );  
    $user = "user_" . $author_id;
    $acf_info = get_field($field, $user);
    return $acf_info;
Advanced Scripts plugin

Next, edit your posts template. To retrieve your ACF author field, you need to use Dynamic Data > PHP Function Return Value. Enter the function name my_get_user_field, and in the arguments box, enter the name of the ACF field that you want to retrieve.

You can see an example of how to do this here:

You should then see your ACF data shown in your Oxygen template.

Tutorial written by Phe Simmonds
Director & Web Developer at WinuSoft Web Design & Senior Support Specialist at Oxygen. When she's not working, Phe can usually be found mountain biking with her partner, Luis.

Get in Touch

Contact us about any web, design or branding project that you have in mind. If you're after custom development services for your Oxygen Builder site, we can help with that too!

We're sure that you'll find our approach and dedication will be just what you need. We’re always happy to discuss your project with you. Drop us an email or give us a call to get started.