Display posts by one author and only from current category (wordpress)

I need your help to solve my problem.

So, what I can’t make:
For instance, I have a category ‘NEWS’, this category has some subcategories ‘POLITICS’, ‘SPORT’, ‘LIFE’ etc.

Read More

In the sidebar displayed a list of author’s names and if I click for some author in some category I need to show only posts written by this author and from the current category, subcategory.

How can I create it? I suppose that it’s able to create by pre_get_posts but not sure.

Related posts

Leave a Reply

1 comment

  1. I found the solution.

    Display all users that even had been written posts in certain category (I created a carousel slider of it’s avatars/names). Then I generate the link and add current category at the end.

    <ul id="carousel">                                                                                                                                                                                                      
    <?php
    $current_category = single_cat_title("", false);
    $author_array = array();
    
    $args = array(
    'numberposts' => -1,
    'category_name' => $current_category,
    'orderby' => 'author',
    'order' => 'ASC'
    );
    $cat_posts = get_posts($args);
    foreach ($cat_posts as $cat_post) :
    if (!in_array($cat_post->post_author,$author_array)) {
    $author_array[] = $cat_post->post_author;
    }
    endforeach;
    foreach ($author_array as $author) :
    $auth = get_userdata($author)->display_name;
    $langCur = pll_default_language();
    $auth_link = get_userdata($author)->user_login;
    $homeurl = home_url();
    
    echo "<li>";
    echo "<a href='".$homeurl."/author/".$auth_link."/?cat=".get_cat_ID( $current_category )."' title=''>";
    echo get_avatar( $author, $size = '140' );
    echo "<span>";
    echo $auth;
    echo "</span>";
    echo "</a></li>";
    endforeach;
    ?>
    </ul>