0

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

);

2

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)

  • hmmm... I'm using a plugin to create my meta-boxes. link So it would be hard for me to post the code for when the meta-box is set or saved. I was trying to figure out how to extend this plugin, because it is writen in OOP, but I couldn't figure it out. So I gave up and that's why I'm trying to grab the meta-data this way. Is there any way I can grab the meta-data once it's entered, but before it is saved? – Caleb Feb 1 '12 at 20:20
  • I realize another way I could get my function to work is to simply sort the posts and display them according to the meta-date instead of the post_date. I really wanted to get it working with the post_date though. :( – Caleb Feb 1 '12 at 20:26
  • Just post what you send into the plugin, that should be sufficient...if the plugin is handling all the save functions for the metadata, then you should just be able to get it via $_POST – mor7ifer Feb 1 '12 at 20:45
  • ok, just added the code in an edit... – Caleb Feb 1 '12 at 21:32
  • 1
    See what $_POST['cfc_date'] gives you, it should be exactly what's entered in the Day field if my hunch is correct. – mor7ifer Feb 1 '12 at 21:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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