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

I've tried linking it to all these filters but none seem to work...

It works, except I have to press the "Update" button twice to change the post_date to match my new meta date.

What am I doing wrong?

share|improve this question
    
I'm gonna try to find a solution here, but the issue is that the hook you're using is calling the action it's hooked to, so, as you suspected, you are creating an infinite loop...hopefully the solution lies in using a different hook. –  m0r7if3r Jan 31 '12 at 1:40

3 Answers 3

up vote 1 down vote accepted

A Note: Actions and Filters are not really interchangeable: Filters typically must return the passed data or it's going to break something.

Building off of what m0r7if3r said, wp_insert_post_data is a filter, so you should be modifying the post's $data and returning it at the end of the function.

(Alternatively, you could global the variables you need to alter during an appropriate action, then change it.)

for a tutorial, first read: http://core.trac.wordpress.org/browser/tags/3.3.1/wp-includes/post.php#L2566

Then consider the following (I haven't tested this but it's based on your code):

function cfc_reset_postdate( $data ) {
    $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');
share|improve this answer
    
hey, thanks for writing in that helps me about filters. I'll try what you mentioned and let you know how it works. –  Caleb Feb 1 '12 at 3:15
    
Ok, so I've updated my question to show my new code I'm trying. It works except I have to press the "Update" button twice to get the post_date to change. Any ideas? –  Caleb Feb 1 '12 at 16:06
    
any idea on why I have to press the "Update" button twice for my function to take effect? –  Caleb Feb 1 '12 at 16:14
    
Sounds like your post is saving first, then the saving of 'cfc_date' is occuring... So the first time you hit update it works, but updates the post with the old cfc_date value, then updates the cfc_date. Then the next time you update the new value is used. Hopefully, understanding this sequence can lead you to a solution. –  WraithKenny Feb 16 '12 at 15:54

I've got a loose implementation for you. Look into the 'wp_insert_post_data' filter, it is the last filter/action executed that has access to the data being inserted. You should be looking at receiving just the first parameter, which will be the array of data, changing that, then returning it. You will have access to all the post data that is about to be inserted, so if you need to do checks (or, in your case get_post_meta()) this would be a good time to do them. This filter executes regardless of whether the post is a new post, or an update...but other than that there aren't many caveats to it. If you need to look at source, its on line 2567 of /wp-includes/post.php

share|improve this answer
    
Ok, thankyou for the info... I'll look into it and let you know what I come up with. Remember the real trouble I'm having is changing the date of the post. I can't believe no one has ever written a tutorial on this - I think I'll do that once we figure this out!!! –  Caleb Jan 31 '12 at 3:09
    
Shouldn't be too tricky, just avoid using wp_insert_post() and wp_update_post(), modify the data directly. Something that did not come to mind is that you'll probably have to use your sanitized data, you won't be able to pull it from the database, since the post won't have been created yet. Post creation comes later, so you'll want to use the data you PLAN on putting as meta. Just out of curiosity, why are you duplicating core functionality? –  m0r7if3r Jan 31 '12 at 10:38
    
Basically I'm trying to create a church program that allows me to build a jQuery dropdown calendar which the user can use to select the date for the sermon with. It makes it easier for them to use a dropdown that has all the days grayed out except Sundays. Just a user-friendly feature I'm putting in... btw thanks for the help, I've been a little busy today, but I'll post again once I've tried to implement what you said. –  Caleb Feb 1 '12 at 3:14
    
I've updated my question to show what I tried. Any ideas on why I have to press the "Update" button twice to save my meta date changes? –  Caleb Feb 1 '12 at 16:15
    
Are you using the incoming meta values, or the saved meta values? Create a new question and post your code and you'll get better help than trying to drill down into comments...and it'll be better for the community as well as the organization of the site. Don't be shy with making questions, that's what this site is all about. –  m0r7if3r Feb 1 '12 at 18:51

I know this is question was asked a while ago, but just incase someone was wondering, an easier way of doing this would be:

<?php $date = get_post_meta($post->ID, "cfc_date", true);
echo the_time('Y-m-d' , $date );?>

Check out Wordpress Codex

share|improve this answer

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.