Home › Forums › General Issues › Check if someone checked a checkbox › Reply To: Check if someone checked a checkbox
You can use the acf/save_post action to check if the checkbox is checked and save the current time to the post meta. Then you can check the current time against the saved time in your template.
Save the current time when the post is saved:
<?php
function my_acf_save_post( $post_id ) {
// Your checkbox field.
$checkbox_field = get_field( 'checkbox', $post_id );
// The post meta you'll be creating.
$checkbox_time = get_post_meta( $post_id, 'checkbox_saved_time', true );
/*
* Add the current time to the post meta if your checkbox field is checked
* and checkbox_saved_time doesn't already have a value saved.
*/
if ( $checkbox_field && ! $checkbox_time ) {
update_post_meta( $post_id, 'checkbox_saved_time', strtotime( 'now' ) );
}
}
add_action( 'acf/save_post', 'my_acf_save_post', 20 );
?>
Compare the time in your template:
<?php
$checkbox_time = get_post_meta( $post_id, 'checkbox_saved_time', true );
if ( $checkbox_time < strtotime( '-24 hours' ) ) {
// The checkbox was checked within the last 24 hours.
}
?>
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.