I'm having a problem with updating the post_date in a custom function I've written.
I'm trying to change the "post_date" to my custom "meta_date" value.
Here is the function:
function cfc_reset_postdate( $data, $postarr ) {
// If it is our form has not been submitted, so we dont want to do anything
if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;
if($data['post_type'] == 'scripture-memory') {
$date = get_post_meta( get_the_ID(), 'cfc_date', true );
$date = DateTime::createFromFormat('D - M j, Y', $date);
$date = $date->format('Y-m-d');
$data['post_date'] = $date;
return $data;
}
add_filter( 'wp_insert_post_data', 'cfc_reset_postdate', '10', 2);
It works, except I have to press the "Update" button twice to change the post_date to match my new meta date.
I read something in wp-includes http://core.trac.wordpress.org/browser/tags/3.3.1/wp-includes/post.php#L2380 on line 2380 "If the $postarr parameter has 'ID' set to a value, then post will be updated." Not sure what that means, but I think it may help me solve this problem.
What am I doing wrong?
EDIT:
Here is the code I'm sending into the meta-box plugin:
$prefix = 'cfc_';
global $meta_boxes;
$meta_boxes = array();
// 1st meta box
$meta_boxes[] = array(
// Meta box id, UNIQUE per meta box
'id' => 'scripture_memory_verse',
// Meta box title - Will appear at the drag and drop handle bar
'title' => 'Scripture Memory Verse',
// Post types, accept custom post types as well - DEFAULT is array('post'); (optional)
'pages' => array( 'scripture-memory' ),
// Where the meta box appear: normal (default), advanced, side; optional
'context' => 'normal',
// Order of meta box: high (default), low; optional
'priority' => 'high',
// List of meta fields
'fields' => array(
array(
// Field name - Will be used as label
'name' => 'Reference',
// Field ID, i.e. the meta key
'id' => $prefix . 'reference',
// Field description (optional)
'desc' => 'If you don\'t spell the book correctly, the verse won\'t show up!',
// CLONES: Add to make the field cloneable (i.e. have multiple value)
'clone' => true,
'type' => 'text'
),
array(
'name' => 'Day',
'id' => "{$prefix}date",
'type' => 'date',
'desc' => '(What Sunday are we saying this verse)',
// Date format, default yy-mm-dd. Optional. See: http://goo.gl/po8vf
'format' => 'DD - M d, yy'
)
)
);