简体   繁体   中英

WordPress: Get terms from author

On the author archive page I want to show every taxonomy (in my case product category) in which the author has posts (in my case WooCommerce products).

To do so, I'm using the following code:

$posts = get_posts( array('post_type' => 'product', 'posts_per_page' => -1, 'author' => $author_id) );
$author_categories = array();
//loop over the posts and collect the terms
foreach ($posts as $p) {
    $author_categories = wp_get_object_terms( $p->ID, 'product_cat');

    if ( ! empty( $author_categories ) && ! is_wp_error( $author_categories ) ){
        echo '<div class="d-flex flex-wrap">';
            foreach ($author_categories as $author_category) {
                //var_dump($t);
                $author_category_link   = get_term_link( $author_category );
                $author_category_name   = $author_categories[] = $author_category->name;

                echo '<a href="'.esc_url( $author_category_link ).'" class="p-2 p-lg-5 bg-light text-center">';
                echo $author_category_name;
                echo '</a>';
            }
        echo '</div>';
    }
}
wp_reset_postdata();

The problem is, that the product category appears multiple times. I guess for every post in that product category.

Is there any way to collect the terms first and display them only once ordered by the count of posts in the product category?

You should first collect all categories in a container array. By using the ID or the name of the category as the array key when adding categories to the array, you eliminate duplicates. Then you just loop over the resulting array to echo the categories.

// First just collect the distinct categories.
$posts = get_posts( array('post_type' => 'product', 'posts_per_page' => -1, 'author' => $author_id) );
$all_author_categories = array();
foreach ($posts as $p) {
    $author_categories_for_p = wp_get_object_terms( $p->ID, 'product_cat');
    if ( ! empty( $author_categories_for_p ) && ! is_wp_error( $author_categories_for_p ) ){
        foreach ($author_categories_for_p as $author_category) {
            $all_author_categories[$author_category->name] = $author_category;  
        }
    }
}

// Then just loop over the collected categories and display them.
echo '<div class="d-flex flex-wrap">';
foreach($all_author_categories as $author_category) {
    $author_category_link   = get_term_link( $author_category );
    $author_category_name   = $author_categories[] = $author_category->name;

    echo '<a href="'.esc_url( $author_category_link ).'" class="p-2 p-lg-5 bg-light text-center">';
    echo $author_category_name;
    echo '</a>';
}
echo '</div>';

Remarks:

  1. This code does not scale well when you have a lot of posts. You should use a custom query instead.
  2. I am pretty sure the wp_reset_postdata(); is not needed at the end, because you are not altering the global $post object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM