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

);

share|improve this question

1 Answer 1

up vote 1 down vote accepted

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)

share|improve this answer
    
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 –  m0r7if3r Feb 1 '12 at 20:45
    
ok, just added the code in an edit... –  Caleb Feb 1 '12 at 21:32
    
See what $_POST['cfc_date'] gives you, it should be exactly what's entered in the Day field if my hunch is correct. –  m0r7if3r Feb 1 '12 at 21:37

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.