0

I am trying to display the post count of some taxonomies. I have a custom template that I am showing some taxonomies on. I am using advanced custom fields to assign each repeater field to a taxonomy. How can I display the amount of post for each one?

0

You can use this code for getting number of posts in a taxonomy:

$args = array(
  'post_type'     => 'post',
  'post_status'   => 'publish',
  'posts_per_page' => -1,
  'tax_query' => array(
    array(
      'taxonomy' => 'your-taxonomy-name',
      'field'    => 'slug',
      'terms'    => 'some-slug',
    )
  )
);
$query = new WP_Query( $args );
echo $query->post_count;
  • I put the ACF field name of the sub field where your-taxonomy-name is located and switched post to the post type name. Everything just prints 0. I am using a while loop and have multiple taxonomies. – JediTricks007 May 2 '15 at 0:24
  • Do you want to get number of posts in a taxonomy at all or number of posts with a term from a taxonomy? – Emetrop May 2 '15 at 0:36
  • I think number of posts in a taxonomy. Would you be kind enough to explain what you mean so I can best answer your question? :) – JediTricks007 May 2 '15 at 1:32
  • 1
    I think he's asking: For instance, if your taxonomy name is "Colors" are you trying to get the number of posts that have ANY colors assigned, or are you trying to get the number of posts with a SPECIFIC color. – Eckstein May 2 '15 at 3:00
  • In this case that code should be OK. Just replace 'your-taxonomy-name' with your taxonomy name, slug with 'slug' with 'color' and 'some-slug' with 'red' for example. – Emetrop May 3 '15 at 21:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.