Home › Forums › Add-ons › Options Page › Save repeater field as custom post type › Reply To: Save repeater field as custom post type
No problem 🙂
Ah okay that’s a bit different then.
Setting it up as you suggest wouldn’t really change anything technically but it would reduce the confusion of having to save the options page before seeing the new events in the contacts repeater.
To save your events as a cpt you can do something like:
function my_acf_save_post( $post_id ) {
// get new value
$events = get_field('events', 'options');
//loop through the events repeater
if( $events ){
foreach( $events as $event ){
//Try to find an existing post with the same title. If it exists we dont need another
$exists = get_page_by_title( $event['eventname'], OBJECT, 'eventcpt' ); //make sure eventcpt and eventname are correct
if( !$exists ){
//didnt exist so lets create one.
wp_insert_post(array(
'post_title' => $event['eventname'],
'post_status' => 'publish',
'post_type' => 'eventcpt'
));
}
}
}
}
// run after ACF saves the $_POST['acf'] data
add_action('acf/save_post', 'my_acf_save_post', 20);
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.