• Hi all,

    Every post on my website is in two categories. A City category and a Letting Agent category. These are the top level categories and they both have many subcategories, which are the city names and letting agent names.

    Basically what I am hoping to do is query the posts from my selected city category and display the number of letting agents that are operating in this city.

    I would like to do this anywhere on the website. So far I have the City category in a variable called $city.

    So something like.

    1. query_posts(“cat=$city”);
    2. Find the Letting agent subcategories which these posts belong to.
    3 Display the number of these subcategories.

    Note I am not trying to find the number of posts, but the number of categories. WordPress doesn’t have an inbuilt function :-(.

    Thanks for your help!

Viewing 2 replies - 1 through 2 (of 2 total)
  • $sections = get_categories(array(
       'parent' => get_cat_id('<category-slug>')
    ));
    echo count($sections);

    Replace ‘<category-slug>’ with the appropriate slugs. If you already have the category ID of the parent cats you can skip the get_cat_id part and just use 'parent' => <cat-ID>. I think that will give you what you want.

    Thread Starter scdwb

    (@scdwb)

    Thanks for your help apljdi.

    I have used the code with the ID and have:

    $sections = get_categories(array(
    'parent' => 50));
    echo count($sections);

    This displays the total number of subcategories of ID 50, which is the letting agent category and there are 20.

    What I need to do is somehow use this code to display the number of subcategories of ID 50 being used by the posts in my chosen city category.

    So in php could we say something like:

    1. Query posts from “city name” category
    2. Then displays similar code to above to display the amount of letting agents active in this city.

    Thanks, this is getting so close.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Interesting category counting question.’ is closed to new replies.