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've been working on a custom post type meta-box implementation that allows me to change the post_date to match the value in my "date" meta-box.

The function works perfectly except when I try to add a new custom post, I receive a blank page! I know it's this function because if I remove it, I can create a new post no problem.

Here is my function:

function cfc_reset_postdate( $data ) {
 // 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 = $_POST['cfc_date'];
    $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', '99', 1);

Any ideas on why I'm receiving a blank page when trying to create a new post?

EDIT:

I've changed my code to reflect your ideas:

function cfc_reset_postdate( $data ) {

//if($data['post_type'] == 'scripture-memory') {
    if($_POST['cfc_date'] != '') {
        $date = $_POST['cfc_date'];
        $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', '99', 1);

Only problem is when I create a new post, it only allows me to create a draft, and then it comes up with a blank page that says "Are you sure you want to do that?"

I'm going to work on this more, it looks like we're making progress. Thanks everybody for your help!

share|improve this question
    
Please describe what this function is supposed to do. Looking at it I might guess one thing ... but I highly doubt that's what you intended... –  EAMann Feb 1 '12 at 22:17
    
what happens if you remove the DOING_AUTOSAVE line and maybe you should add a check if your $_POST data is set. Not sure, but i guess wp_insert_post_data runs when you create a new post as well, and by now, since your cfc_date is not set, you have no valid value for post_date. Don't know how worpress feels about that :) –  ungestaltbar Feb 1 '12 at 22:43
    
Does the entire page still break if you comment out the second if statement? (/*if($data['post_type']...){...}*/) –  m0r7if3r Feb 1 '12 at 22:53
    
ok, so I made some changes to the code - getting closer to the correct code. –  Caleb Feb 2 '12 at 1:40
    
Coming even closer I moved "return $data" out of all the if statements and it's working better. Problem is now when I publish the post it gives the blank page. At least it publishes correctly though... –  Caleb Feb 2 '12 at 14:47
show 2 more comments

1 Answer 1

up vote 0 down vote accepted

Awesome - I got it working!

Thanks everybody for your help and comments. Turns out the function had a problem because it was taking the $data in, but never returning it, because I had "return $data" in my if statement!

Another problem was with the "WPSearch 2" plugin", it was breaking my "New Post" functionality - strange :(

Here is the completed correct function:

function cfc_reset_postdate( $data ) {
    if($data['ID']) { $postarr['ID'] = $data['ID']; }

    if($data['post_type'] == 'scripture-memory') {
        if($_POST['cfc_date']) {
            $date = $_POST['cfc_date'];
            $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', '99', 1);

Hope this function helps anybody who wants to change the post_date with a custom meta-box date.

share|improve this answer
    
Then mark the question as answered. –  toscho Feb 6 '12 at 11:29
1  
Thanks, man sorry about that - I forgot :) –  Caleb Feb 7 '12 at 12:25
add comment

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.