Skip Navigation

[Resolved] Redirect author archive dynamically to single Custom Posts (representing the author)

This thread is resolved. Here is a description of the problem and solution.

Problem:
How can I redirect from an Author archive to a Single Custom Post dynamically, so that each author archive redirect redirects to the appropriate Single Post?

Solution:
You need custom code for this, which we can not provide here.
You can see an example of another Toolset user in the ticket below
https://toolset.com/forums/topic/author-archive-vs-team-member-cpt/#post-1454025

100% of people find this useful.

This support ticket is created 4 years, 11 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

This topic contains 7 replies, has 2 voices.

Last updated by geophray 4 years, 11 months ago.

Assisted by: Beda.

Author
Posts
#1452009
views-issue_02.jpg
views-issue_01.jpg
views-issue_00.jpg

Tell us what you are trying to do?
I've created a staff CPT as outlined in the following support thread: https://toolset.com/forums/topic/displaying-dynamic-content-on-author-archive-using-elementors-template-builder/

I chose to create this CPT for the staff/author profiles so that I could associate reviews with each staff member, which is not currently possible to do with users. However, I want to have a custom author archive (single) that will show all the posts they are the author of and also display their reviews, and other relevant data pulled from the custom fields of their profile, which is stored in the CPT post as opposed to user fields.

It works correctly for one of my test authors, but not for the other. Each of them are administrators that are authors of published profiles. In the view query filter, I am filtering by "author the same as the current post in the loop".

Is there any documentation that you are following?
No, not really. I've looked at several pages but haven't seen anything exactly like what I'm trying to accomplish yet.

Is there a similar example that we can see?
I'd be happy to share access to my site. I can share access credentials if you can enable the private reply mode. It has "Privacy Mode" enabled with the host, so to access even the front end you need a username/password.

What is the link to your site?
hidden link

#1452107

I failed to mention that I'd like to have the team member profile page redirect to the author archive page as well. Is this possible with toolset?

#1452145

I think I discovered what the problem was... It was that only one of the users had a blog post published. I changed the author on that post and it basically reversed which author archive worked as expected.

So as a follow-up, I think I may reverse the direction I'm going with this and have the CPT template for these pages have a section that will display any articles that exist within that template so it acts as the author archive. Is there a way that you are aware of that I can easily redirect the author archive pages to the appropriate profile post? I just want to make sure any links to the author archive get redirected to the staff members CPT profile instead.

#1452597

Redirects cannot be done with Toolset, you'd need custom code or another plugin to do this.

Do I understand it correctly that you successfully built the Post Type and as well the list of posts "authored" by those Post Types (which mimic users), and the only issue remaining is to redirect from the Archive of that post Type to the single post of a specific "author"?

That would then require custom code that we cannot provide here.
I am not even sure this is possible, given on an archive, there will always be "many" posts, so "many" authors in your case, and redirecting could then at most be done manually, as one could not dynamically pick and decide which "author" in the archive list to redirect to.

This will always be a problem when mimicking users into a post type, that the post type becomes its archive automatically and you can only style that archive, but not change how it works in the core, using Toolset.
What you can do, is totally disable the archive. In Toolset > your_post_type > Edit > Options you can uncheck "has_archive", which then simply makes sure no archive is produced for that post type, hence, avoiding the need of any redirect.

Would this help?

#1453253

Hi Beda,

Thanks so much for the fast reply. I wasn't expecting to hear back from you until after the weekend. 🙂

Yes, you are correct in that I have successfully built the post type and I do have a few sample posts. However, I must not have been as clear when describing which archive I would like to redirect. I want to redirect the author archive to the profile post of the respective author, for which there will only be a single post of that type authored by them.

I've come up with the following code to try and accomplish this redirect but I'm still pretty green when it comes to PHP... can you see where I'm going wrong right off the bat?

add_action( 'template_redirect', 'redirect_author_archive_to_staff_profile' );

function redirect_author_archive_to_staff_profile() {

    if ( is_author() ) {
        $author = get_the_author();
        if($author) {
            $args = array(
                'author' => $author->ID,
                'post_type' => 'staff',
                'post_status' => 'publish'
            );
            $author_posts = new WP_Query( $args );
            if( $author_posts->have_posts() ) {
                $authorProfileUrl = get_permalink($author_posts[0]);
                wp_reset_postdata(); 
            }
        }
        wp_safe_redirect( $authorProfileUrl, 301 );
        exit;
    }
}
#1453793

Ok... after working on this some more I have updated my code to be the following, and while it is redirecting to one of the CPT posts, it is not taking the author of the current author archive into consideration and is just loading the first published post of type "staff" regardless of the author.

add_action( 'template_redirect', 'redirect_author_archive_to_staff_profile' );

function redirect_author_archive_to_staff_profile() {

    if ( is_author() ) {
        $author = get_the_author();
        if($author) {
            $args = array(
                'author' => $author->ID,
                'post_type' => 'staff',
                'post_status' => 'publish',
				'posts_per_page' => 1
            );
            $author_posts = new WP_Query( $args );
            if( $author_posts->have_posts() ) {
				$author_posts->the_post();
				$author_bio_post = get_the_ID();
                $authorProfileUrl = get_permalink($author_bio_post);
                wp_reset_postdata();
            }
        }
        wp_redirect( $authorProfileUrl, 301 );
        exit;
    }
}

I realize this is not really your job, so if you aren't able to help me with this I completely understand.

#1454025

I figured it out! Here is what I ended up with for my redirect function:

add_action( 'template_redirect', 'redirect_author_archive_to_staff_profile' );

function redirect_author_archive_to_staff_profile() {

    if ( is_author() ) {
        $author = get_the_author_meta('ID');
        if($author) {
            $args = array(
                'author' => $author,
                'post_type' => 'staff',
                'post_status' => 'publish',
                'posts_per_page' => 1
            );
            $author_posts = new WP_Query( $args );
            if( $author_posts->have_posts() ) {
                while( $author_posts->have_posts() ) {
                    $author_posts->the_post();
                    $author_bio_post = get_the_ID();
                    $authorProfileUrl = get_permalink($author_bio_post);
                }
                wp_reset_postdata();
            }
        }
        wp_redirect( $authorProfileUrl, 301 );
        exit;
    }
}
#1454027

My issue is resolved now. Thank you!