I'm building a website that has "Team Members" section and "Blog" section. Team members are a custom post type.
The client requested that the links to authors in blog posts should point to associated Team Member page instead of the default author page in WordPress (basically they don't want to have default author pages at all).
I've found this solution offered by @Damocles - Use "Team Members" custom post type instead of Author for blog posts
Basically the solution proposed by him was simple and exactly as I thought initially of tackling this issue:
- Create a "Post Object" ACF field and set it to filter through "Team Member" custom post types
- Attach this field to User accounts
- Go to user profile and choose the correct Team Member from drop down menu
- Then use a filter in functions.php to automatically replace the author link everywhere with associated Team Member url
Makes sense but unfortunately, it doesn't want to work on my website. I even used the same name for ACF field as he did and used the exact same code in functions.php:
add_filter( 'author_link', 'team_author_link', 10, 3 );
function team_author_link( $link, $author_id, $author_nicename ) {
$team_post_id = get_field('team_post', $author_id);
// if the team post is set, get the permalink to the team post:
$team_link = get_permalink($team_post_id);
$link = ($team_link !== false) ? $team_link : $link;
return $link;
}
The author link DOES change, BUT instead of pointing to the associated Team Member page, all author links point to the currently opened blog post URL. I don't know, maybe my theme is overwriting the query or something, so the URL to the custom post type cannot be achieved from blog post view?
Can someone help me achieve it, please? I want to attach a Team Member (custom post type) to user account in WordPress and replace the author link through functions.php to the associated Team Member page url.
CodePudding user response:
It's because you are missing a prefix, to tell ACF that it should be looking on a user.. - try this
$team_post_id = get_field('team_post', 'user_'.$author_id);
Docs are here : https://www.advancedcustomfields.com/resources/how-to-get-values-from-a-user/