WordPress.org

Ready to get started?Download WordPress

Forums

Count how many posts tagged with a tag (1 post)

  1. drabiej
    Member
    Posted 5 years ago #

    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 :)

Topic Closed

This topic has been closed to new replies.

About this Topic