I would like to find a function that will automatically change a posts date to the modified post date.
I’m using wp User front end plugin so that visitors can add and edit a custom post. I’m also using the Post expirator plugin so that their post will automatically be set to draft after 1 month. The user will then be emailed and asked to update their post for it to go back on line. The problem is that the plugin only sees the post date. So once a user logs back in and edits their post (using front end user) the expirator date isn’t reset to be 1 month from the modified date, instead it still uses the original date. Hope that all makes sense.
So my choices are to either try and write my own function to handle the post expiration based on the modified date or to try and force the posts date to change to the same as the modified date once it has been updated.
Can anyone help me with either solution? I’m guessing the latter solution would be the most straight forward to implement but willing to head advice.
Many thanks
D
I’ve tried the suggestions below but have not managed to get this to work. Can anyone offer a solution to this?
Thanks again!
D
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Doing what you ask is remarkably easy.
function reset_post_date_wpse_121565($data,$postarr) {
// var_dump($data,$postarr); die; // debug
$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 answer with hesitation though. As in my comment, if you are using dates in your permalinks you will generate broken links every time that post_date changes.
The real problem in your case seems to be the design of the “expirator” plugin which would mean that the proper solution might be to redesign that plugin, or find a filter that it provides, instead of altering something like the post date (which has consequences).
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0