save_post action has empty $_POST?
-
I’m trying to reformat the date entered into a custom field called event_date_begin so that it’s in a standard format when the post is published. Every related bit of information I can find says to hook into the save_post action and retrieve existing custom field values with $_POST[‘custom field name’]. Yet I always find this has no contents, even though I know this custom field exists. This is in my functions.php file:
add_action('save_post', 'update_dates'); function update_dates ($ID = false, $post = false) { if (isset($_POST['event_date_begin'])) update_post_meta($ID, 'event_date_begin', date('Y/m/d', strtotime($_POST['event_date_begin']))); else update_post_meta($ID, 'event_date_begin', 'empty'); }This always sets rewrites the event_date_begin custom field to “empty” (the else clause here is just for testing).
I even once put in code to check $_POST[‘event_date_begin’] right after setting it to the ’empty’ value, and it still fails the isset() test.
I can’t find anyone else saying $_POST is empty when it shouldn’t be, so I can’t figure out why it’s always empty for me. This is when I update a post (as opposed to a page, which I haven’t tested).
-
Dont check for isset of $_post here as you want to update your meta value.
check for empty $_POST for the meta key and the update as you want .if ($_POST['event_date_begin']!='') update_post_meta($ID, 'event_date_begin',date('Y/m/d', strtotime($_POST['event_date_begin']))); else update_post_meta($ID, 'event_date_begin', 'empty');and please check that meta key you used is correct.
I was struggling with a similar strange problem with a site that otherwise worked. It all worked on a different host, too – the development host. But deployed to production, every form had an empty $_POST and was not treated like a POST, even though I could see a POST in the webserver logfiles.
I eventually found the cause, accidentally, when solving what seemed like a lower priority problem. The error logfile for the Apache server was complaining about “Negotiation: discovered file(s) matching request: … (None could be negotiated)”.
When I fixed that (removed the Option for MultiViews in the Apache Virtual Server configuration file), I got $_POST filled in.
So it may be a subtle server configuration problem.
The topic ‘save_post action has empty $_POST?’ is closed to new replies.