I am not a coding person at all (very basic HTML and CSS) but I have been asked by a client to have a field in each post they make (just standard WP posts, not custom post types or anything) which holds the year that the post was created. This was easy enough using ACF and the field now shows up underneath the main content area when editing the post. What I am having no luck figuring out is how to automatically fill that field with the year when the post is saved (initially saved...not updated).
I got some info from ACF about using the acf-save_post action and the "applied before save" version...and they also directed me to the Wordpress Developer Resources site for using get_the_date. The trouble is, I have NO idea what I am doing with PHP! What I tried piecing together is below with (I presume) the function name being "acf_post_year" and the ACF field name being "post_year":
add_action('acf/save_post', 'acf_post_year', 5);
function acf_post_year( $post_id ) {
// Get previous values.
$prev_values = get_fields( $post_id );
// Get submitted values.
$values = $_POST['acf'];
// Check if a specific value was updated.
if( isset($_POST['acf']['post_year']) ) {
function get_the_date( $format = 'Y', $post = null ) {
$post = get_post( $post );
if ( ! $post ) {
return false;
}
$_format = ! empty( $format ) ? $format : get_option( 'date_format' );
$the_date = get_post_time( $_format, false, $post, true );
/**
* Filters the date a post was published.
*
* @since 3.0.0
*
* @param string|int $the_date Formatted date string or Unix timestamp if `$format` is 'U' or 'G'.
* @param string $format PHP date format.
* @param WP_Post $post The post object.
*/
return apply_filters( 'get_the_date', $the_date, $format, $post );
}
}
}
Obviously it didn't work and I don't even know where to start! So if anybody can give me a solution that I can just drop in to functions.php (using Code Snippets as I currently am) that would be amazing!! In return for you time and knowledge, you are more than welcome to mock me for taking on something I clearly have no ability to finish!!!