Skip to content

Instantly share code, notes, and snippets.

@mafsdisseny
Created May 30, 2016 16:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mafsdisseny/b3f6c26e83a26b29a52e0fcb375cdb95 to your computer and use it in GitHub Desktop.
Save mafsdisseny/b3f6c26e83a26b29a52e0fcb375cdb95 to your computer and use it in GitHub Desktop.
ACF custom fields: Auto populate end date with the start date value, when it is empty.
<?php
// Auto-populate end date if it is empty.
function mafs_update_data_final( $value, $post_id, $field ) {
//NOTE: don't use get_field() because it retrieves the value
//in a preformatted way different as it is saved in database
$data_final = get_post_meta( $post_id, 'doc_data_final', true );
$data_inicial = get_post_meta( $post_id, 'doc_data_inici', true );
//Check if data_final is empty (when saving) and data_inicial
//has a value registered, then assign it to the empty one.
if ($data_final == '' && $data_inicial != '') {
$value = $data_inicial;
}
return $value;
}
// Write the name of the affected custom field in the filter
// in our case: doc_data_final
add_filter('acf/update_value/name=doc_data_final', 'mafs_update_data_final', 10, 3);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment