Inicio › Toolset Professional Support › [Resuelto] Automatic post delete
This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.
Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.
Sun | Mon | Tue | Wed | Thu | Fri | Sat |
---|---|---|---|---|---|---|
- | 7:00 – 14:00 | 7:00 – 14:00 | 7:00 – 14:00 | 7:00 – 14:00 | 7:00 – 14:00 | - |
- | 15:00 – 16:00 | 15:00 – 16:00 | 15:00 – 16:00 | 15:00 – 16:00 | 15:00 – 16:00 | - |
Supporter timezone: Europe/London (GMT+00:00)
Etiquetado: Setting up custom fields, Types plugin
Documentación relacionada:
Este tema contiene 11 respuestas, tiene 2 mensajes.
Última actualización por Nigel hace 6 años, 4 meses.
Asistido por: Nigel.
hey there i am trying to build:
A post type which will be deleted after 60 days (i know where i can do this in toolset)
But now the tricky part:
In the formto that post type i want to have a date field where the front end user can put in the custom post delete date. Then the 60 days should be overwritten with the date of the front end user.
When a user doesn’t fill the date field out the 60 days rule should delete it automatically.
Hope you can help!
Thank you
Idiomas: Inglés (English ) Español (Español )
Zona horaria: Europe/London (GMT+00:00)
Hi Steffen
You are familiar with the post expiration feature of Toolset Forms.
If you have a form with the setting "posts expire 60 days from now" then that will be applied to all posts submitted with the form.
Once such posts are submitted, if you edit the posts in the backend you will see when the post is due to expire, and you can update it from the post edit screen when you save the post.
The expiry date is stored as a UNIX timestamp in wp_postmeta for the post, with the key "_cred_post_expiration_time".
So you can customise this using the cred_save_data hook (https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data).
You could have a field in the form to choose how long the post should last before expiring, and then when the form is submitted you run a little code with the cred_save_data hook which updates the _cred_post_expiration_time post meta according to what was chosen in the form.
Is that something you can do? If you get stuck let me know.
Hey nigel thanks for your reply,
sorry but i am a beginner with no coding skills. could you help me out with an example?
the screenshots are showing the post type with automatic delete and the date field in the form.
Maybe we can go through this what i have to do?
Thanks for your support.
cheers
Idiomas: Inglés (English ) Español (Español )
Zona horaria: Europe/London (GMT+00:00)
Hi Steffen
You already have the default 60 day expiration set up in your form.
You'll need to add the following code to your theme's functions.php or using a plugin such as Code Snippets:
/** * Set custom expiry date based on form field */ function tssupp_custom_expiry( $post_id, $form_data ){ $expires_field = 'wpcf-self-destruct'; if ( 6 == $form_data['id'] ) { // Edit form ID if ( isset( $_POST[ $expires_field ] ) { $expires = $_POST[ $expires_field ]; $new_expiration = time() + $expires * 24 * 60 * 60; update_post_meta( $post_id, '_cred_post_expiration_time', $new_expiration ); } } } add_action( 'cred_save_data', 'tssupp_custom_expiry', 10, 2 );
You will need to edit the field which sets the number of days when the post should expire (note that Types fields are stored with a 'wpcf-' prefix), as well as the ID of the form.
For testing, submit the form with some number of days different than 60 (e.g. 1), then edit the post in the backend where you will be able to see the post expiration date which should correspond with the value from the field in the form.
Hey Nigel thanks for the code. but i get an error everytime i want to save it:
syntax error, unexpected '{'
function tssupp_custom_expiry( $post_id, $form_data ){ $expires_field = 'wpcf-bewerbungsende'; //Name of the field if ( 6 == $form_data['39'] ) { // form ID is 39 if ( isset( $_POST[ $expires_field ] ) { $expires = $_POST[ $expires_field ]; $new_expiration = time() + $expires * 24 * 60 * 60; update_post_meta( $post_id, '_cred_post_expiration_time', $new_expiration ); } } } add_action( 'cred_save_data', 'tssupp_custom_expiry', 10, 2 );
cheers
Idiomas: Inglés (English ) Español (Español )
Zona horaria: Europe/London (GMT+00:00)
Sorry, I introduced a typo with a last-minute edit.
This should work:
/** * Set custom expiry date based on form field */ function tssupp_custom_expiry( $post_id, $form_data ){ $expires_field = 'wpcf-self-destruct'; if ( 6 == $form_data['id'] ) { // Edit form ID if ( isset( $_POST[ $expires_field ] ) ) { $expires = $_POST[ $expires_field ]; $new_expiration = time() + $expires * 24 * 60 * 60; update_post_meta( $post_id, '_cred_post_expiration_time', $new_expiration ); } } } add_action( 'cred_save_data', 'tssupp_custom_expiry', 10, 2 );
hey Nigel,
thanks al lot for your help.
I just realised i need to flip this because:
I want to built a job posting.
So the date field in the front end should be the one which brings the ctp post type after "the date which is selected" in privat mode (so the post type is gone for all other users except the Author, right?).
After the cpt "job" is in privat mode it should automatically delete the post after x days.
Could you help me with this?
Idiomas: Inglés (English ) Español (Español )
Zona horaria: Europe/London (GMT+00:00)
You want that
1. when the post is submitted that after the time specified in the form field the post should become private, and
2. after some other interval the post is deleted
1. is straightforward. Simply edit the form settings and change the setting "After expiration change the status of the post to:" to Private.
2. is not so easy.
I have set up a test site with an attempt at a solution, but I need to wait a little while for the post expirations to work their way through to know if it works correctly or not.
If it does I'll probably split that into a separate thread so I can describe the problem and solution in one place that others may find helpful.
I'll update you.
Great!!!!!
thank you very very much!
Los hilos nuevos creados por Nigel y vinculados a este se encuentran a continuación:
Idiomas: Inglés (English ) Español (Español )
Zona horaria: Europe/London (GMT+00:00)
Unfortunately it doesn't work 🙁
I was trying to use the hook cred_post_expiration_custom_actions (https://toolset.com/documentation/user-guides/automatic-post-expiration/#what-happens-when-posts-expire) to reset the expiration to add a new expiration time and action to effectively chain a second activation onto the second one, but it failed, it seems like the expiration time is reset to zero *after* the custom actions are performed, and so as-is, it is simply not possible (without writing an entirely bespoke system).
So, only one expiration action, which in your case is to set the post status to private after the interval the user specifies in the form.
I'm sorry I couldn't get the second part to work. I'm going to split this thread so that I can make a feature request to add another hook that happens later that would make this possible.
Thank you sooooo much nigel for testing it!
Idiomas: Inglés (English ) Español (Español )
Zona horaria: Europe/London (GMT+00:00)
No problem, it was an interesting challenge, but I don't think there is anything else I can do here, so please go ahead and close the thread if you are happy to.