WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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,

$excluded_categories would be an array containing: 1,2,3,4,5,6,8,9
share|improve this question
up vote 7 down vote accepted

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);
share|improve this answer
Sweet, never knew about that function (t31os here, on secondary system), real handy that one... :) – Mark Duncan Jul 16 '11 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 '11 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 '11 at 13:33
@andy just use implode(), that is purely PHP part and has little to do with WP. – Rarst Jul 16 '11 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 '11 at 22:29

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;

Hope that helps...

share|improve this answer
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 '11 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? – Mark Duncan Jul 16 '11 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 '11 at 22:43
No problem, i'd support Rarst's approach anyway.. ;) – t31os Jul 17 '11 at 8:58

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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