سؤال

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

);

هل كانت مفيدة؟

المحلول

The issue is that you are using get_post_meta(), but since you're hooking wp_insert_post_data you have not set the post meta yet (because the hook fires before the post is inserted into the database. You should be pulling from the metadata submission, and running through any filters you use for update_post_meta(). If you post the code where you set the postmeta and comment back, I'll try to write out some sample code (assuming you need it)

نصائح أخرى

Try to see if you can grab the $_POST data and add it when update:

//see what is being pass...
print_r($postarr);
die();

Then add the field info to your function

function update_post_from_meta($data,$postarr) {

//see what is being pass...
//print_r($postarr);
//die();

if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;

if($data['post_type'] == 'scripture-memory') {

$date = $_POST['you_field_name'];
//OR
//$date = $postarr['your_field_name'];

$date = DateTime::createFromFormat('D - M j, Y', $date);
$date = $date->format('Y-m-d H:i:s');

$data['post_date'] = $date;
return $data;

}
add_filter('wp_insert_post_data','update_post_from_meta',99,2);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى wordpress.stackexchange
scroll top