I'm currently using this to add post meta

    add_post_meta($post_id, 'when', $date);

Naturally, it creates a new meta key/value combination each time.

I want to another value to the key, so that when date is submitted again, there are now TWO dates in the "when" meta key?

Would I then be able to query these dates? Or would storing them in the same meta key cause them to be unreadable later?

Eventually the goal is to count how many entries there are in the meta key, while still being able to process the dates as dates.

Is this possible or should a new method of storage be used?

2 Answers 2


Use the following instead of add_post_meta:

update_post_meta($post_id, 'when' $date);

add_post_meta does just that, adds a new meta key every time, including duplicates in your case.

update_post_meta also incorporates add_post_meta if a key doesn't exist, but updates an existing key.

For reference: http://codex.wordpress.org/Function_Reference/update_post_meta

If you want to store rolling dates in an array, you would simply create the array before storage like so:

$dates = get_post_meta($post_id, 'when', true);

    $dates[] = $new_date; //I'm sure you would do more processing here
    $dates = array($new_date);

update_post_meta($post_id, 'when', $dates);

I would recommend that you create a custom interface for this rather than using the default Custom Fields interface and rename your custom field to '_when' so it doesn't appear in the main interface. Since you're saving an array, it wouldn't be really practical to display it to the user in raw form.

Check out these two references for more info in creating a meta box:

  1. http://codex.wordpress.org/Plugin_API/Action_Reference/save_post
  2. http://codex.wordpress.org/Function_Reference/add_meta_box
  • what Im asking is will add another value to the key, but keeping the old value(s) as well.
    – marctain
    Commented Jul 19, 2012 at 4:40
  • Check out the latest edit. Commented Jul 19, 2012 at 4:44

I had this problem and this is how I solved it :

In my case, I wanted to add an array to the existing serialized data :

enter image description here

I wanted to add the new array data posted using Ajax to the existing meta_key.

Note : Existing meta_key is 'accordions'

function save_new_data(){

  // Step 1: get the data you want to add (can be from an ajax post like my case)

     $posted_data = $_POST;

     //clean it up like remove what you don't want like the action from ajax post  

      $new_data  = array_diff_key($posted_data, ['action' => "xy", ]); //optional

   //Step 2: get the existing data

      $existing_data = get_post_meta(get_the_ID(), "accordions", true);

   //Step 3: check if its an array, add new data to the existing data then update_post_meta()

    if(is_array($existing_data)) {

         $existing_data[] = $save_data; 

           update_post_meta(get_the_ID(), 'accordions', $existing_data );


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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