Tell us what you are trying to do?
Hello, I've created a field group and added it to standard WP-posts.
How can I set date field value for today while creating/editing post in WP-Admin. Please see screenshots attached//
Thank you1
What is the link to your site? hidden link
Hi, you can add this custom code to your child theme's functions.php file. This will automatically set the custom field date whenever a Post is created or edited:
add_action( 'save_post', 'set_sht_date_field_to_today', 100, 3 );
function set_sht_date_field_to_today( $post_id, $post, $update ) {
if ( $post->post_type == 'post' ) {
update_post_meta( $post_id, 'wpcf-sht-event-date_from-new', date('U') );
}
}
This will overwrite any date that already exists in the date field, and change it to today's date.
Dear Christian, is it possible to add if field empty condition... I mean, no need to overwrite in case not empty...
Thanks!
Sure, try this variation:
add_action( 'save_post', 'set_sht_date_field_to_today', 100, 3 );
function set_sht_date_field_to_today( $post_id, $post, $update ) {
if ( $post->post_type == 'post' ) {
$date = get_post_meta( $post_id, 'wpcf-sht-event-date_from-new', true);
if( $date ) {
return;
}
update_post_meta( $post_id, 'wpcf-sht-event-date_from-new', date('U') );
}
}