Skip Navigation

[Closed] Uncheck checkbox in specific time.

This support ticket is created 2 years, 8 months 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 1 reply, has 2 voices.

Last updated by Christian Cox 2 years, 8 months ago.

Assisted by: Christian Cox.

Author
Posts
#2144381

Dears,

I have two fields which are
1. expiration date (single line)
2. account status (Checkbox, if selected --> stores Active , if not selected --> stores Inactive)
when the admin activates the new user account, the status will be Active and the expiration date field calculates and stores the expired date.

I would like to uncheck the account status field to be Inactive when the expiration date = current date.

Could you please help me.

#2145179

I would like to uncheck the account status field to be Inactive when the expiration date = current date.
Hello, it sounds simple, but it is actually rather complex. To automate a task in WordPress that runs periodically, you can use the WP Cron API:
https://developer.wordpress.org/reference/functions/wp_schedule_event/

The task you create should trigger custom code that finds all the posts with an expiration date field equal to Today and a checkbox field that is checked, then unchecks the checkbox field in all those posts.

I can show you how to get the value of a custom field using PHP, and I can show you how to uncheck a checkbox custom field in PHP. To get the value of a custom field, like the expiration date field, use the WordPress API get_post_meta:

$post_id = 12345; // the post ID
$expiry_field_slug = 'expiration-text-field-slug'; // the expiration text field slug
$expiry = get_post_meta( $post_id, 'wpcf-' . $expiry_field_slug, true ); // the value of the expiration text field

Toolset Types custom field slugs have a prefix of wpcf- in the database, so you can see I have added it to the slug in my code. If the expiration field has text like 08/17/2021, you could set up a Query Filter for that custom field and compare it to today's date in the same date format. It could get more complex if you store a full date and time, or if you need to support multiple date formats. You'll probably need to use our wpv_filter_query API to set that filter up correctly, since the date filtering options built-in to the Query Filter system are designed to work with date fields, not text fields.
https://toolset.com/documentation/user-guides/views/filtering-views-by-custom-fields/
https://toolset.com/documentation/user-guides/views/date-filters/
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

To uncheck a checkbox field, the code depends on the field configurations. Usually checkbox fields are set to "When unchecked, don't save anything to the database" by default, since this option works best with custom search Views. To uncheck a checkbox with this default option "...don't save anything to the database" selected, you would delete the postmeta value like so:

$post_id = 12345; // the post ID
$checkbox_field_slug = 'account-status-slug';
delete_post_meta( $post_id, 'wpcf-' . $checkbox_field_slug );

So you can see it's pretty complex and involves a significant amount of custom code. Toolset Forms includes some built-in expiration features and an API for changing a custom field value from X to Y upon post expiration. This feature supports a radio field or a select field but not a checkbox, since it does not help you delete a checkbox field. It only allows you to change the field value from X to Y.
https://toolset.com/course-lesson/setting-up-automatic-post-expiration/#what-happens-when-posts-expire
https://toolset.com/documentation/programmer-reference/cred-api/#cred_post_expiration_custom_actions

The topic ‘[Closed] Uncheck checkbox in specific time.’ is closed to new replies.