How to query posts from specific authors and categories using WP_query?

I currently have a WP_query where I am getting posts from a specific set of authors. To this, I want to add specific categories as well.

$args = array( 'author__in' => $authors, 'posts_per_page' => 12, 'paged' => $paged );

$authors is an array containing users’ ids.

So, I need to query posts from both, authors and categories.

I was thinking about using something like this:

'tax_query' => array(
    'relation' => 'OR',
    array(
        'taxonomy' => 'category',
        'field' => 'id',
        'terms' => array ( $cat_ids ),
    ),
    array(
        'taxonomy', => 'user',
        'field' => 'id',
        'terms' => array( $user_ids ),
    )
)

I know user is not a taxonomy; however, I am looking for something similar that works.

EDIT

Well, I tried this and it is working so far:

$args = array(
  'author__in'     => $authors,
  'category__in'   => $terms,
  'posts_per_page' => 12,
  'paged'          => $paged
);

Is there a better way?

EDIT 2

My previous edit is not working the way I wanted. The caveat is that when only categories are specified, no posts show up.
I need an OR relation.

EDIT 3

I havent been able to find a solution. When I use author_in and category_in for the arguments, wordpress only shows posts from authors and not both. I need to show posts from authors AND categories. What am I doing wrong here?

3 Answers
3

Is this what you are looking for?

$query = new wp_query($arr);
    $arr = array(
        'author__in'=> array(2,4,6), //Authors's id's you like to include
        'posts_per_page' => '12',
        'paged' => $paged,
        'tax_query' => array(
        array(
        'taxonomy' => 'category',
        'field' => 'id',
        'terms' => array ( $cat_ids ),
        )
    )
    );

Leave a Comment