0

I have the following code that prints a comma separated list which I need in the form of an array, how do I get the list of id's into an array called $excluded_categories with contents as per below?

$categories = get_categories(array('taxonomy'=>'projects_category', 'exclude'=>7, 'orderby'=>'id'));
if ( !empty( $categories ) ) :
foreach($categories as $term){
    echo $term->term_id.', '; // prints: 1,2,3,4,5,6,8,9,
    }
endif;

$excluded_categories would be an array containing: 1,2,3,4,5,6,8,9

2 Answers 2

8

WP has a lot of nifty functions in the corners for such stuff. Try this:

if ( !empty( $categories ) ) {

    $excluded_categories = wp_list_pluck( $categories, 'term_id');
    $excluded_categories = implode(',', $excluded_categories);
}
5
  • Sweet, never knew about that function (t31os here, on secondary system), real handy that one... :)
    – t31os
    Jul 16, 2011 at 12:33
  • Cheers for that Rarst, it simplifies the code a bit! But it looks like that still returns an array with all the category details rather than just a string of ID's, how can I strip it down to something that looks like '1,2,3,4,5,6,8,9'?
    – Andy
    Jul 16, 2011 at 13:19
  • my mistake, it produces an array of just the ID's, I now need to get that to give a string of comma separated id's to insert into another function...
    – Andy
    Jul 16, 2011 at 13:33
  • @andy just use implode(), that is purely PHP part and has little to do with WP.
    – Rarst
    Jul 16, 2011 at 13:45
  • Thanks Rarst, I will do that. I realised as soon as I'd commented that it was probably just a bit of php and nothing more to do with wordpress.
    – Andy
    Jul 16, 2011 at 22:29
3

I'm pretty sure if you switched to get_terms or similar you could return the data as an array and have no need to loop over it and build the array.

However, having said that(and to save me going to check the docs), you can do it like so..

$categories = get_categories(array('taxonomy'=>'projects_category', 'exclude'=>7, 'orderby'=>'id'));
if ( !empty( $categories ) ) :
$excluded_categories = array();
foreach( $categories as $term ) {
    $excluded_categories[] = $term->term_id;
}
endif;

Hope that helps...

4
  • Cheers for that Mark, it still returns an array rather than just a string of comma separated ID's, how can I strip it down to something that looks like '1,2,3,4,5,6,8,9' that I can then use as a range of ID's to exclude from another function?
    – Andy
    Jul 16, 2011 at 13:32
  • You said "I have the following code that prints a comma separated list which I need in the form of an array", which is exactly what was given to you, your original code provided a comma seperated list, which is it you want, an array of ids or a string of ids seperated by commas?
    – t31os
    Jul 16, 2011 at 13:48
  • Sorry Mark, my mistake. I was echo-ing the wrong variable, didn't realise until after I'd posted my comment, just like with the answer above. Your code did what I was asking for, I have voted it up. Thank you.
    – Andy
    Jul 16, 2011 at 22:43
  • No problem, i'd support Rarst's approach anyway.. ;)
    – t31os
    Jul 17, 2011 at 8:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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