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?
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 asked Jul 19, 2012 at 3:46 marctainmarctain 5673 gold badges8 silver badges19 bronze badges2 Answers
Reset to default 3Use 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/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:
- http://codex.wordpress/Plugin_API/Action_Reference/save_post
- http://codex.wordpress/Function_Reference/add_meta_box
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 :
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 );
}
}