最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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?

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?

Share Improve this question edited Feb 20, 2014 at 20:48 Gixty asked Feb 12, 2014 at 14:12 GixtyGixty 1,0972 gold badges19 silver badges37 bronze badges 5
  • author__in is the way to do it, but that looks like what you started with. – s_ha_dum Commented Feb 12, 2014 at 14:28
  • yes, but now I want to get the posts from specific categories as well in the same query. – Gixty Commented Feb 12, 2014 at 14:35
  • There is very limited user search capability to WP_Query. Sorry. It is not clear what you want. There are multiple ways to interpret your question. Please enumerate your conditions very carefully. – s_ha_dum Commented Feb 12, 2014 at 14:46
  • ok, let me try to clear it up. I want to query posts based on categories and authors ids in the same query. – Gixty Commented Feb 12, 2014 at 14:48
  • hi, are you working with post type "post"? and taxonomy "category"? – Aamer Shahzad Commented Nov 22, 2016 at 2:35
Add a comment  | 

3 Answers 3

Reset to default 1

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 ),
        )
    )
    );

You could try something like this... get_posts_by_author_sql

Alternatively, if you want to select posts based on authors/categories you could use wpdb. You may be able to use a "JOIN" in the SQL to get the post IDs you need.

maybe you can try this in backend:

wp-admin/edit.php?author=2 //author is specified
wp-admin/edit.php?cat=16,25&author=2 //include categories

this test does not appear as you describe, so check whether the value is correct

$terms = array(16,25);
//$author = array(2);
$args = array(
    'author__in'  => isset($author) ? $author : 0,
    'category__in' => isset($terms) ? $terms : ''
    );
$demos = new WP_Query($args);
echo "<pre>";
print_r($demos);
echo "</pre>";
发布评论

评论列表(0)

  1. 暂无评论