Home Forums Add-ons Options Page Save repeater field as custom post type


Save repeater field as custom post type

  • Hi,

    I created an options page and added a subpage to it.
    On the subpage I added a repeaterfield called ‘Events’.
    The field ‘Events’ contains a couple of fields e.g. ‘Eventname’, ‘Eventdate’, etc.

    I added another repeater field to my subpage (same level as ‘Events’) named ‘Contacts’. This one contains a couple of fields e.g. ‘Contactname’, ‘Events’.
    What I want to accomplish is that when I have to fill in the field ‘Events’ under ‘Contacts’, I want to choose one of the events I created earlier in the repeaterfield above.

    Anyone who has an idea to make this work?

    Thanks in advance,

  • Hi Alex,

    It would be hard to do this without saving the options page between putting in the events and then putting in contacts.

    However. You could do a check on loading of the “events” field inside contacts wether there exists some values in the Events repeater and add them as choices as a select field (for example).

    Here’s a snippet you can use as a base that should get you rolling:

    function my_acf_load_field( $field ){
    	$events = get_field('events', 'options'); //the repeater for events
    	if( $events ){
    		foreach( $events as $event ){
    			$field['choices'][$event['eventname']] = $event['eventname'] . ' (' . $event['eventdate'] . ')';
        return $field;
    // acf/load_field - filter for every field
    add_filter('acf/load_field/field_key_for_events_subfield_here', 'my_acf_load_field');
  • Thanks Jonathan for the quick response!

    Would it be easier when my setup would be like this:

    Events (optionspage)
    — Events (subpage)
    —- Event (repeaterfield)
    — Contacts (subpage)
    —- Contact (repeaterfield)

    Basically I want to whenever I create a new event, I want it to be saved as a custom post type ‘event’. So when I go to the ‘Contacts’ page and I create a new contact, I can just search in the custom post type ‘event’.


  • 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);
Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘Save repeater field as custom post type’ is closed to new replies.