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

wp query - Counting number of posts with Category B in Category A

programmeradmin2浏览0评论

I'm currently building my own backlog tracker and have gotten to a point where I just have no idea how I'm going to do this.

I have multiple status categories (for example; beaten and completed) and categories for the consoles (for example PS4 and Nintendo Switch). Now I want to display how many games(posts) I have beaten (Category A) for the Nintendo Switch (Category B)

For Example; I have 66 games, 34 from those are from the Nintendo Switch. From those 34 Switch games, I have actually beaten 24. I want to display this "24".

can anyone give me an idea of where to go and how to fix this problem?

I'm currently building my own backlog tracker and have gotten to a point where I just have no idea how I'm going to do this.

I have multiple status categories (for example; beaten and completed) and categories for the consoles (for example PS4 and Nintendo Switch). Now I want to display how many games(posts) I have beaten (Category A) for the Nintendo Switch (Category B)

For Example; I have 66 games, 34 from those are from the Nintendo Switch. From those 34 Switch games, I have actually beaten 24. I want to display this "24".

can anyone give me an idea of where to go and how to fix this problem?

Share Improve this question edited Apr 23, 2019 at 23:04 Nicolai Grossherr 18.9k8 gold badges64 silver badges109 bronze badges asked Apr 23, 2019 at 21:49 MiiaruMiiaru 311 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 2

Make use of the WP_Query class, namely the tax_query and fields parameters. Get the count from the $found_posts property. Please note, this is exemplary code.

$query = new WP_Query( [
  'post_type' => 'games',
  'tax_query' = [
    'relation' => 'AND',
    [
      'taxonomy' => 'game_status',
      'field' => 'slug',
      'terms' => [ 'beaten' ],
    ],
    [
      'taxonomy' => 'console',
      'field' => 'slug',
      'terms' => [ 'switch' ],
    ]
  ],
  'fields' => 'ids',
] );

$count = $query->found_posts;
发布评论

评论列表(0)

  1. 暂无评论