0

I have an old blog with lots of posts,

I created 2 new custom fields "Year" and "Month" using ACF.

I would like to auto populate them from the post date after post save/update, is this possible ?

Is there a function I can run to do this to all my old posts as well ??

0

Ok, I solved this with a nice function, now the only thing left to do is to find a way to update all of the blog posts. doing this by hand will be really bad

here is the code that I used

add_action( 'save_post', 'myplugin_save_postdata' );

function myplugin_save_postdata( $post_id ) {

if ( 'page' == $_POST['post_type'] ) { if ( ! current_user_can( 'edit_page', $post_id ) ) return; } else { if ( ! current_user_can( 'edit_post', $post_id ) ) return; }

$mydata = get_the_time('Y'); // Do something with $mydata $mymonth = get_the_time('F');

update_post_meta( $post_id, 'year_', $mydata ); update_post_meta( $post_id, 'month', $mymonth ); }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.