1

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

1

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.

  • Thanks guys, it's much closer, I'm seeing about 7 categories now in the array vs. only one. But, there's categories missing from the array... I can't quite determine the pattern for the categories missing either. I'll modify the above code to show you everything I'm using now. – Jonah Aug 8 '14 at 16:35
  • Need to move the $i = 0; outside of the while loop, see my updated answer. – Chris Quinn Aug 8 '14 at 21:10
  • That works Chris, thanks so much! Be happy to buy you a beer if you have a link :) – Jonah Aug 8 '14 at 21:16
0

EDIT

You are saving the categories in the array. This is very good. I am changing this block on code. See the modified code

    $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[] = array( 'slug' => $cat->slug );
        }

    } //endwhile
    $result = array_unique($cats);
   print_r($result);

There have array_unique function. Using this function you can get unique value. See the tutorial here for better knowledge.

  • Hi Chinmoy, thanks for the reply. It doesn't work. I get back just one category. If I print out the $cats array instead, I only get the categories for the last post in the loop. So this is part of the problem... I want to get all the categories for all the posts in my loop, combine them, remove duplicates and end up with that. – Jonah Aug 7 '14 at 18:29
  • ooops... I made mistake. I am editing my code again. So Please check the edited code once. Need to put the $cats = array(); outside the while loop. – Chinmoy Kumar Paul Aug 7 '14 at 19:11
  • Hmmm, thanks but it still doesn't work. I'm only getting one category returned. See my modified code above ^ – Jonah Aug 7 '14 at 21:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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