Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am using the below mentioned code to get top 5 child categories (by number of posts) from a Parent category. The code works fine, however I need to add more "Parent" categories so that I cant get total of Top 5 child categories from multiple parent categories.

Adding 'parent' => '599,588,590',  **Does not work.**

The working code for single parent category.

<?php
$args=array(
  'orderby' => 'count',
  'order' => 'DESC',
  'parent' => '599',
  'number' => '5'
  );
$categories=get_categories($args);
  foreach($categories as $category) {
    echo '<li><a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "" ), $category->name ) . '" ' . '>' . $category->name.' (' . $category->count .')</a></li>';
 }
?>
share|improve this question
add comment

1 Answer 1

up vote 1 down vote accepted

You cannot get it in one call.

Instead, make multiple calls, then sort the data each call gave you into a single array, and take the top 5 in the new list.

e.g.

$args=array(
  'orderby' => 'count',
  'order' => 'DESC',
  'parent' => '599',
  'number' => '5'
  );
$categories=get_categories($args);

$args2=array(
  'orderby' => 'count',
  'order' => 'DESC',
  'parent' => '588',
  'number' => '5'
  );
$categories2=get_categories($args2);

// etc...

Finally you would create a new array that you will use to display the top 5 categories. You will fill this array by searching through the 5 category arrays you have just acquired, and picking out the categories with the highest count..

Think of each array as a top 10, and you're compiling a new top 10 given 5 lists. You'd find the highest category in all the 5 lists, then the next highest, and once you've got 5 items you'd stop and write down your final list.

share|improve this answer
    
I'm not sure if I explained the question right or you understood what I meant. I need to get top 5 "Child Categories" by number of posts from Multiple Parents. I do not want a "child category" to be listed under multiple parents. –  Naveen Narale Mar 25 '13 at 14:38
    
I've ammended my answer –  Tom J Nowell Mar 25 '13 at 14:41
    
Yeh, that is exactly my problem, I understand that it needs multiple call but unsure of how that can be achieved. Can't we simply create an array of parents also? –  Naveen Narale Mar 25 '13 at 15:17
    
No, it would give the same result as passing in comma separated list. –  Tom J Nowell Mar 25 '13 at 15:20
    
Thanx, What would be the last part of the code where all array are stored and top 5 categories are calculated? I have no clue! –  Naveen Narale Mar 27 '13 at 14:01
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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