0

I purchased a theme from themeforest recently that has a custom post type. Here is a link to a page that's been created with the custom post type:

http://bluepresley.com/farmerfinder/?dir-item=dance-relax

The custom post type has approximately 20 different custom fields, but you can see from my array dump that the one one field has been saved as serialized data:

(array dump using get_post_custom())

Array
(
    [_edit_last] => Array
        (
            [0] => 1
        )

    [_ait-dir-item] => Array
        (
            [0] => a:20:{s:7:"address";s:51:"276 Canal Street, New York, NY 10013, United States";s:11:"gpsLatitude";s:17:"40.71958306715651";s:12:"gpsLongitude";s:17:"-74.0379810333252";s:14:"showStreetview";a:1:{s:6:"enable";s:6:"enable";}s:18:"streetViewLatitude";s:9:"40.719679";s:19:"streetViewLongitude";s:18:"-74.03797400000002";s:17:"streetViewHeading";s:18:"-151.8987176681924";s:15:"streetViewPitch";s:17:"5.196265482127373";s:14:"streetViewZoom";s:1:"0";s:9:"telephone";s:13:"044 802 52578";s:5:"email";s:19:"directory@email.com";s:3:"web";s:17:"www.aitthemes.com";s:11:"hoursMonday";s:9:"8am - 8pm";s:12:"hoursTuesday";s:9:"8am - 8pm";s:14:"hoursWednesday";s:9:"8am - 8pm";s:13:"hoursThursday";s:9:"8am - 8pm";s:11:"hoursFriday";s:9:"8am - 8pm";s:13:"hoursSaturday";s:6:"closed";s:11:"hoursSunday";s:6:"closed";s:18:"alternativeContent";s:33:"What goes in alternative content?";}
        )

    [_thumbnail_id] => Array
        (
            [0] => 8159
        )

    [_edit_lock] => Array
        (
            [0] => 1409208357:1
        )

    [slide_template] => Array
        (
            [0] => 
        )
)

Please note the key in question is _ait-dir-item.

I noticed in the form itself all form fields were named name="_ait-dir-item[address]" (for example).

If this is the case, how do I programmatically add items? Is this typical Wordpress and there is a way to handle this approach? I assume I'm not going to be able to use add_post_meta( $post_id, 'mymetakey', $my_long ); or other approaches found here and here. ?

1

For serializing and unserializing Data in Wordpress, you can use maybe_unserialize and maybe_serialize. If you want to handle the data and add/change values, you could use something like this:

$metaarray = maybe_unserialize(get_post_meta($post_id,'_ait-dir-item',TRUE));
//now $metaarray has an array, if it is serialized. If not, $metaarray has a string.
if(is_array($metaarray)){
     $metaarray['mynewfield'] = 'Some text i need to add';
     $metaarray['mysecondnewfield'] = array('an' => 'array','i' => 'need', 'to' => 'add');
}
update_post_meta($post_id,'_ait-dir-item',maybe_serialize($metaarray));

Happy Coding, Kuchenundkakao

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.