admin管理员组

文章数量:1023848

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;

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;

本文标签: wp queryCounting number of posts with Category B in Category A