$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Batch Extract Date from post title and put into ACF custom field|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Batch Extract Date from post title and put into ACF custom field

matteradmin11PV0评论

I have a custom post type called 'events' with around 700 posts, the titles are consistently formatted like this:

Event Title - January 1st 2019

I need to isolate the date in another field. I am using Advanced custom fields and have added a date field to the events custom post type.

I need to write a script to extract the date from each title and insert it into the custom field.

How would I do this? I have tried writing SQL statements but just cant get it to work. I need some guidance.

I have a custom post type called 'events' with around 700 posts, the titles are consistently formatted like this:

Event Title - January 1st 2019

I need to isolate the date in another field. I am using Advanced custom fields and have added a date field to the events custom post type.

I need to write a script to extract the date from each title and insert it into the custom field.

How would I do this? I have tried writing SQL statements but just cant get it to work. I need some guidance.

Share Improve this question asked Feb 16, 2019 at 14:52 onei0120onei0120 394 bronze badges 3
  • Have you looked into wp_ update_post and update_post_meta? – birgire Commented Feb 16, 2019 at 17:28
  • @birgire these filters won't solve the problem. I think that OP has added a new field (when he already have many posts) and now he want's to automatically modify all of old posts so that they use this new field. But maybe I'm getting it all wrong... – Krzysiek Dróżdż Commented Feb 16, 2019 at 21:16
  • thanks @KrzysiekDróżdż, I meant the functions within some loop, but was I was writing on mobile and didn't have time to find the parentheses, sorry about that :-) Hopefully OP can further add what's been tried. – birgire Commented Feb 16, 2019 at 21:54
Add a comment  | 

1 Answer 1

Reset to default 1

OK, so here's the function that should do the trick:

function modify_events_to_use_new_field() {
    $events = get_posts( array(
        'post_type' => 'event',
        'post_status' => 'any',
        'posts_per_page' => -1
    ) );

    foreach ( $events as $event ) {
        if ( preg_match('@^(.*) - ([^-]*)$@', $event->post_title, $matches) ) { 
            // you'll have to modify the POST_META_KEY to the real name of your custom field
            update_post_meta( $event->ID, '<POST_META_KEY>', date('Ymd', strtotime($matches[2]) ) );
        }
    }
}

Now you have to change the name of custom field in there and run that function.

Post a comment

comment list (0)

  1. No comments so far