WordPress.org

Support

Support » How-To and Troubleshooting » Count how many posts tagged with a tag

Count how many posts tagged with a tag

  • Searched and can’t find an answer to this simple question:
    How to count the number of posts which are tagged with a tag?
    Where the tag may not exist or there may be no posts tagged with it.

    I’d like a function that does this:

    $n = num_posts_tagged("my_tag");
    //$n is the number of posts tagged 'my_tag'

    Right now I have this ugly, slow workaround:

    query_posts('tag=' . 'my_tag');
    $n  = 0;
    while (have_posts())  {
      the_post();
      $n++;
    }

    Anyone have anything more elegant?

    Thanks 🙂

  • The topic ‘Count how many posts tagged with a tag’ is closed to new replies.