Skip Navigation

[Resolved] Automatically delete the attached file

This support ticket is created 3 years ago. There's a good chance that you are reading advice that it now obsolete.

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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 3 replies, has 3 voices.

Last updated by Luo Yang 3 years ago.

Assisted by: Luo Yang.

Author
Posts
#2558123

Tell us whIn the backend I have a "article field group" where I entered a file upload.
Is there a function that allows you to automatically delete the attached file in the front-end after 15 days?

#2558161

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi there

There isn't such a feature available, and to implement something like this would require quite a bit of custom coding.

If you are knowledgeable about PHP then we can give you some pointers as to how you might add such a feature, though we cannot code it for you. If you are not then you would need to recruit a developer to do that for you. You can find developers who know Toolset well on our contractors page: https://toolset.com/contractors/

If you are handy with PHP and want to try it yourself, let us know and we can point you in the right direction.

#2558713

Thank you, I would like to have more information on how to customize the code!

#2559139

In my opinion, you can try these:
1) Trigger a CRON job every day using a custom PHP function:
https://developer.wordpress.org/plugins/cron/

2) In this PHP function:
Get all "article" posts IDs which are older than 15 days:
https://developer.wordpress.org/reference/classes/wp_query/#date-parameters

3) Use above "article" posts IDs to get the custom file field values:
https://developer.wordpress.org/reference/functions/get_post_meta/

4) Use above file field values to get the attachment IDs:
https://developer.wordpress.org/reference/functions/attachment_url_to_postid/
And delete them:
https://developer.wordpress.org/reference/functions/wp_delete_attachment/