Get the terms of a custom taxonomy for a specific author in author template

I am using a custom post type in my website and a custom taxonomy for it.

An author selects one or many terms before publishsing its post.

My goal is to display authors pages in front end, so I am using author.php template file. This file is by default displaying the archive of posts written by the specific author. How can I add to this file the list of the custom taxonomy terms for the author published posts?

I give the following example if I wasn’t clear in my explanation:

if Author-x has published:

**post1** with term1 , term2, term3
**post2** with term2, term5
**post3** with term1


then, in Author-x page I will have : term1, term2, term3, term5.

It is exactly the same principle as in user page in stack exchange. As you can see, there is a list of tags for each user which are tags of posts the user contributed in.

Thank you for your usual help.

1 Answer
1

First get a list of the author posts then loop over the each post and get the terms used ex:

function list_author_used_terms($author_id){

    // get the author's posts
    $posts = get_posts( array('post_type' => 'custom_post_type_name', 'posts_per_page' => -1, 'author' => $author_id) );
    $author_terms = array();
    //loop over the posts and collect the terms
    foreach ($posts as $p) {
        $terms = wp_get_object_terms( $p->ID, 'taxonomy_name');
        foreach ($terms as $t) {
            $author_terms[] = $t->name;
        }
    }
    return array_unique($author_terms);
}

//usage
echo implode(", ",list_author_used_terms(1));

Leave a Comment