0

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";}
}
2
  • isn't ce4_category_fields a array? use implode if you want it as a string... 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
2

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;
}
0

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.