Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm just starting to store data in multi-dimensional arrays in wp_options and I need some help to retrieve some data...

My data is below and is stored as option_name "my_category_fields"

I need to pull a string of category ids in which the my_cat_hide is set to true. I've stubbed my function here. How do I complete it?

function get_hidden_cats()
{
$my_cats = get_option('ce4_category_fields');
    //how do I capture the list of category ids where my_cat_hide is 'true'?
    $my_hidden_cats = ?
    return $my_hidden_cats;
}

my_category_fields:

a:17:{
    i:20;a:1:{s:13:"my_cat_title";s:30:"Black Tea is Good for the Soul";}
    i:9;a:1:{s:13:"my_cat_title";s:58:"Herbal Chinese Tea is a spectacular blend of herbs and tea";}
    i:44;a:1:{s:13:"my_cat_title";s:24:"This is my 9th category!";}
    i:7;a:1:{s:13:"my_cat_title";s:0:"";}
    i:19;a:1:{s:13:"my_cat_title";s:0:"";}
    i:4;a:2:{s:13:"my_cat_title";s:28:"My test Title for Chai Tea 4";s:12:"my_cat_hide";N;}
    i:37;a:1:{s:13:"my_cat_title";s:0:"";}
    i:16;a:1:{s:13:"my_cat_title";s:0:"";}
    i:5;a:2:{s:13:"my_cat_title";s:0:"";s:12:"my_cat_hide";N;}
    i:6;a:1:{s:13:"my_cat_title";s:0:"";}
    i:8;a:1:{s:13:"my_cat_title";s:0:"";}
    i:3;a:1:{s:13:"my_cat_title";s:0:"";}
    i:10;a:1:{s:13:"my_cat_title";s:59:"Iced Tea: The Great Southern Choice for Cooling Refreshment";}
    i:36;a:1:{s:13:"my_cat_title";s:28:"Full title for this category";}
    i:38;a:2:{s:13:"my_cat_title";s:0:"";s:12:"my_cat_hide";s:4:"true";}
    i:39;a:2:{s:13:"my_cat_title";s:0:"";s:12:"my_cat_hide";s:4:"true";}
    i:40;a:2:{s:13:"my_cat_title";s:0:"";s:12:"my_cat_hide";s:4:"true";}
}
share|improve this question
    
isn't ce4_category_fields a array? use implode if you want it as a string... –  onetrickpony Jan 23 '11 at 7:35
1  
Your code should not look like the second block you've posted above after you've called get_option, because it should be unserializing that data when called(that's how it would typically appear in the DB). –  t31os Jan 23 '11 at 10:28
add comment

1 Answer

up vote 2 down vote accepted

The following should do it:

function get_hidden_cats() {
    $my_cats = get_option('ce4_category_fields');

    $my_hidden_cats = array();
    foreach( $my_cats as $cat_id => $cat_attrs ) {
        if( 'true' == $cat_attrs['my_cat_hide'] )
            $my_hidden_cats[] = $cat_id;
    }
    $my_hidden_cats = implode( ',', $my_hidden_cats );

    return $my_hidden_cats;
}
share|improve this answer
    
Sweet. Thanks as always sorich :) –  Scott B Jan 24 '11 at 4:10
    
You are welcome. ;) –  sorich87 Jan 24 '11 at 7:12
add comment

Your Answer

 
discard

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.