Don’t use global $post
. Everything you need should be passed into the function in the $data
and $postarr
parameters.
Secondly, you have to return
the $data
in all cases. What you are doing now is effectively erasing data for everything but your profile
post type.
You need to reorganize a bit:
function reset_post_date_wpse_121565($data,$postarr) {
if ($data['post_type'] == 'profile'){
$data['post_date'] = $data['post_modified'];
$data['post_date_gmt'] = $data['post_modified_gmt'];
}
return $data;
}
add_filter('wp_insert_post_data','reset_post_date_wpse_121565',99,2);
I have not tested that thoroughly but that is the right idea.
As for a better way…
Your expire_posts
function is not going to be reliably portable as it uses a hard-coded database prefix. At the very least, rewrite that SQL so that the prefix is not hard-coded:
function expire_posts() {
global $wpdb;
$daystogo = "30";
$sql = "UPDATE {$wpdb->posts} SET `post_status` = 'draft' WHERE `post_type` = 'profile' AND DATEDIFF(NOW(), `post_date`) > '$daystogo')";
$wpdb->query($sql);
}
add_action('init', 'expire_posts');
I also just notices that your WHERE
clause has a column that doesn’t exist. There is no the_date
column in the $wpdb->posts
table. It should be post_date
.
Honestly, if you were to deal with the modified date only, you should be able to get this to work without ever altering the post date at all. I don’t think you need that second block of code at all.