i have a custom post type where a post is generated by a form submit. One key is an array which should be stored in a custom field... i shortended the code below to the important part -> they key is "event_musikstil"

'meta_input' => array(                          
             // Custom Fields
                'event_musikstil' => foreach( $event_musikstil as $item ) { add_post_meta( $post_id, "_event_musikstil", $item ); },

You can simply store the meta as an array... Also to hell with add_post_meta... I never use it. update defaults to add if the meta doesn't exist for the post.

update_post_meta($post_id, "_event_musikstil", $event_musikstil);

wordpress will store the value as a serialized array then so just retrieve it like

get_post_meta($post_id, "_event_musikstil", TRUE);

Hope this helps.

Another way, if you don't want to store a serialized array is to use explode and implode

        $metaStr = get_post_meta($post_id, 'your_key', true);
            $meta_arr = explode(',', $metaStr); // assuming you stored as , separated
            $meta_arr = []; // empty array
        foreach($newValuesToAdd as $newValueToAdd) {
            $meta_arr[] = $newValueToAdd;
       update_post_meta($post_id, 'your_key', implode(',',$meta_arr));
| improve this answer | |
  • Curious to the down vote as word-press saves serialized arrays and retrieves with no issue. I supposed I could answer the question using explode and implode method to save as a comma separated string. – Robert Ruby II Oct 30 '19 at 0:00

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.