[Fix] Author Archive Page

tdrayson

The Issue

A known bug confirmed by the Oxygen Team is that Author Archive pages don’t work properly. If the user doesn’t have any posts attached to their account/profile, when you visit their profile URL – mydomain.com/author/{username} and are using the dynamic data → display name. It will output the first user in your website. (Normally user with ID – 1.)

This is obviously incorrect, however the actual author page is created and loaded correctly. We know this because the body has the correct classes added to the page. (See below)

This can cause issues if you want to create profile pages for subscribers or other users that don’t have the ability to create posts or don’t have any post attached to their profile yet.

I spent about 3 whole days trying to figure this out. I finally managed to create a snippet that will allow you to get the correct dynamic data for the author/user page that you are currently on.

Note: I use author and user interchangeably throughout the post. They mean the same thing since this applies to all user roles, not just the author

The Fix


        function tct_author_meta($meta,$acf = ''){
    global $wp_query;
    $curauth = $wp_query->get_queried_object();

    switch ($meta) {
        case 'acf':
            return get_field($acf,'user_'.$curauth->ID);
            break;
        case 'role':
            return = $curauth->roles[0];
        default:
            return $curauth->$meta;
            break;
    }
}
      
Copy

Using The Function

Add your oxygen component (text, image etc) – then use Dynamic Data -> PHP Return Value

Name = tct_author_meta
Arguments = {meta_name}

ACF fields

If you want to use ACF user fields within the author page, then same as before Dynamic Data -> PHP Return Value

Name = tct_author_meta
Arguments = acf,{field_name}

User Role

If you want to return the author/user role within the author page, then same as before Dynamic Data → PHP Return Value

Name = tct_author_meta
Arguments = role

The Breakdown

For those of you who are interested in understanding what the code above does. I have broken it down below.

First we first are creating a function that takes 2 arguments. 1 required, 1 optional.


        function tct_author_meta($meta,$acf = ''){

}
      
Copy

Then we need to create an object for the current query. In our case that is the author/user.


        global $wp_query;
$curauth = $wp_query->get_queried_object();
      
Copy

We now are using a switch statement to return the appropriate response, based on the arguments passed into the function.


        switch ($meta) {
        // If meta = 'acf' then return the field for the user
        case 'acf':
            return get_field($acf,'user_'.$curauth->ID);
            break;
        // If meta = 'role' then return the role for the user
        case 'role':
            return $curauth->roles[0];
        // Else it returns the meta name you passed in
        default:
            return $curauth->$meta;
            break;
    }
      
Copy


Thats it – I hope this can help and save at least one person 3 days trying to figure out a solution.

👋🏻 Weekly Tutorial Digest

I send out a weekly newsletter with links to new tutorials written in the last week, you can subscribe below.

Newsletter

🔒I won't send you spam, I promise