Support » Fixing WordPress » Number of categories in X Category these posts belong to?

  • Hi all,

    All my posts are in 2 categories, one named City with child cat city names and another named Letting Company, with child cat company names.

    When a user selects a City from a drop down menu on the homepage, they are sent to a page with property listings (posts) from that city.

    I would like to tell the user how many letting companies are currently active in this city.

    So my logic is to query the posts in the current city cat and somehow display how many of the Letting Company categories these posts are in.

    Hmm, can you help? Thanks in advance

Viewing 13 replies - 1 through 13 (of 13 total)
  • Thread Starter scdwb

    (@scdwb)

    Can anyone help me out with this? I’ll try and explain it better.

    I need to take all the posts in a selected city category (I have this ready in a variable) and find out the number of subcategories being used in the posts from the Company category.

    For example I may have 10 posts in the London Category. 5 of these posts are also in the “Company1” category and the other 5 are in the “Company2” category.

    So I need to find a way to calculate that there are 2 companies being used in these 10 posts.

    Thread Starter scdwb

    (@scdwb)

    Can anyone point me in the right direction with this?

    Thanks!

    Use Function_Reference/get_categories with the child_of argument and count the elements returned.

    [Please refrain from bumping as per Forum Rules]

    Thread Starter scdwb

    (@scdwb)

    Thanks Michael,

    Im not sure how to make this work though for the posts being queried

    query_posts("cat=$city&showposts=-1");
    $categories=  get_categories('child_of=50');

    How would I count the number of child categories being used within these posts?

    Thanks!

    Thread Starter scdwb

    (@scdwb)

    Thanks again Michael,

    I’m getting closer, this code echos the total number of child categories

    $categories=  get_categories('child_of=50');
    $result = count($categories);

    I’m trying to echo the number of child categories of category id 50, but which are being used in the posts in the $city variable category.

    Something like this, but this code doesn’t work (I have the cat id in the $city variable:

    query_posts("cat=$city&showposts=-1");
    $categories=  get_categories('child_of=50');
    $result = count($categories);

    Can you help me out? Thanks.

    Steve

    <?php
    $post_count=0;
    $categories=get_categories('child_of=50');
    //echo "<pre>"; print_r($categories); echo "</pre>";
    if (! empty($categories) ) {
      foreach($categories as $category) {
        $post_count+= $category->category_count;
      }
    }
    echo 'Count is '.$post_count;
    ?>
    Thread Starter scdwb

    (@scdwb)

    Thanks for your quick reply. At the moment this code is just displaying the total of posts in my site, 265.

    So if I am right, the code is adding up all the mosts in all the category in child of id 50?

    I need it to add up all the child categories being used in the category id 50, but only for the posts in my other chosen category (defined as variable $city).

    IS this possible?

    Thread Starter scdwb

    (@scdwb)

    I have just removed the + from this bit of your code

    $post_count+=

    I can’t find any reference to += anywhere. Was this a type?

    Anyway by removing this the result is always 3 for som reason?

    Use this instead.

    <?php
    $mypost_count=0;
    $categories=get_categories('child_of=50');
    //echo "<pre>"; print_r($categories); echo "</pre>";
    if (! empty($categories) ) {
      foreach($categories as $category) {
        $mypost_count= $mypost_count + $category->category_count;
      }
    }
    echo 'Count is '.$mypost_count;
    ?>

    Thread Starter scdwb

    (@scdwb)

    Nope, its still displaying 265. I am a beginner in PHP but as I read the code all it is calling is the sub categories in “child_of=50” and just adding up all the posts in these sub categories?

    What I need it to do is to just query all the posts in the $city category ($city variable is the ID) and count how many of the “child_of=50” categories are being used in these posts.

    So for example, if 10 posts are in the $city category and 5 of these are in category 1 of “child_of=50” and the other 5 are in category 2 of “child_of=50” then the number that would be displayed is 2.

    It must be possible but im really struggling to find out how to do this.

    Sorry, maybe someone else can understand it…

    Thread Starter scdwb

    (@scdwb)

    OK, thanks for your help. Ill start another thread with a clearer explanation.

Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘Number of categories in X Category these posts belong to?’ is closed to new replies.