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.
    				   '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);