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 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?

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

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);

if(is_array($dates)) 
    $dates[] = $new_date; //I'm sure you would do more processing here
else
    $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
share|improve this answer
    
what Im asking is will add another value to the key, but keeping the old value(s) as well. –  marctain Jul 19 '12 at 4:40
    
Check out the latest edit. –  Brian Fegter Jul 19 '12 at 4:44
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.