EDIT: Moved $i = 0
outside the loop. That should get you the full list of categories.
$get_posts = new WP_Query();
$i = 0;
$get_posts->query($args);
if($get_posts->have_posts()) {
$cats = array();
while($get_posts->have_posts()) { $get_posts->the_post();
$post_categories = wp_get_post_categories( get_the_ID() );
foreach($post_categories as $c){
$cat = get_category( $c );
$cats[$i] = $cat->slug ;
$i++;
}
} //endwhile
$result = array_unique($cats);
print_r($result);
} //endif
wp_reset_postdata();
Basically it flattens the nested arrays in $cats
– I think array_unique
may have been discarding categories because they had an identical key.