Actually I want to have a list of categories in the sidebar of my theme. Only those categories should show up in the list which are assigned to the posts shown in the loop.
So lets say I navigate to main page where all posts show up, the category-list would list all categories. When I navigate to a tag-archive (?tag=anything), the category list should only show those categories which are assigned to the posts in this specific tag-archive. And so on..
I use this function for it in the sidebar:
$cat_array = array();
$args=array(
'posts_per_page' => '-1');
$currentposts = new WP_Query($args);
foreach($currentposts->posts as $currentposts->post) {
foreach(get_the_category($currentposts->post->ID) as $category) {
$cat_array[$category->term_id] = $category->term_id;
}
}
$cat_ids = implode(',', $cat_array);
wp_list_categories('include='.$cat_ids.'&title_li=Author Categories&show_count=1');
When I use $currentposts = $wp_query; instead of the new WP_query, everything works perfect, except that global $wp_query onyl considers a maximum of 10 posts, as this is the number i set at the WP reading settings.