0

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

1
  • If you are using dates in your permalinks you will be constantly creating broken links. Be aware.
    – s_ha_dum
    Commented Nov 7, 2013 at 14:24

1 Answer 1

0

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).

6
  • Hi s_ha_dum Thanks very much for the reply! Is there any way to only apply this to a particular custom post? I agree it's not generally a good idea to mess with the post date but for this particular custom post type it would actually be beneficial. thanks again for the help. D
    – user36987
    Commented Nov 7, 2013 at 16:24
  • Actually, just tried your code and it seemed to break when I tried updating a post. I ended up with a page full of php errors like this link. Any ideas?
    – user36987
    Commented Nov 7, 2013 at 16:44
  • Is anyone able to provide any further assistance with this? I'm afraid I've hit a wall and can't get this figured out. Would greatly appreciate it. Thanks D
    – user36987
    Commented Nov 22, 2013 at 17:30
  • Comment the var_dump. That is debugging only.
    – s_ha_dum
    Commented Nov 22, 2013 at 17:36
  • Ahh. Thank you so much s_ha_dum. That works perfectly! Would you be able to show me how I apply this to one particular custom post type as I don't want this to apply to all posts. Many many thanks!
    – user36987
    Commented Nov 25, 2013 at 17:15

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