0

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!

7
  • 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, 2012 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 :) Feb 1, 2012 at 22:43
  • Does the entire page still break if you comment out the second if statement? (/*if($data['post_type']...){...}*/)
    – mor7ifer
    Feb 1, 2012 at 22:53
  • ok, so I made some changes to the code - getting closer to the correct code.
    – Caleb
    Feb 2, 2012 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, 2012 at 14:47

1 Answer 1

0

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.

1
  • Then mark the question as answered.
    – fuxia
    Feb 6, 2012 at 11:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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