I'm looking to get a plain list of the unique categories in a custom loop (only categories for the posts in the loop). I've been fumbling with some code for a bit and here's what I've got:
<?php
$args = array(
'post_status'=>'publish',
'post_type'=>'post',
'posts_per_page'=>-1
);
$get_posts = new WP_Query();
$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() );
$i = 0;
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();
?>
This will put each posts categories into an array and I can print that out. But I want to merge each posts array of categories, remove duplicates (so there's only one instance of each unique category), and then be able to print it out.
Essentially what I'm wanting to accomplish is to have a drop down for these categories to use for being able to sort by a particular category by refreshing the loop with that selected category. I can handle the rest, I just need to get this list of unique, unduplicated categories for all posts in a loop.
Anybody have any ideas?
Many thanks, Jonah