How to spawn recurrent events with custom field data from parent

I have a custom field for the incsub_event type called: "SoundCrew"
When I create a new recurring event and set this custom field with a value, the spawned recurring events' SoundCrew custom field is blank.

Programmatically, how do I ensure spawns are created with the custom field data of the parent?

(All code must always be upgrade safe!)

  • Sajid

    Hi @Nanjo

    Hope you are doing good today :slight_smile:

    Is it working fine for the single events ? How did you created the custom field ? I mean with code or with default WordPress custom fields interface. If you created it with a code then post that code here.

    Also we have CustomPress plugin to create unlimited custom fields for any post type and much more. May be that would be of some help ?

    Hope that helps :slight_smile: or feel free to post a reply if you need further assistance.

    Cheers, Sajid

  • Sajid

    Hi @Nanjo

    Hope you are doing good today :slight_smile:

    Thanks for further details. I got your point, however, at the moment its not possible out of the box and may require some custom work. I am flagging SLS (Second Line Support) staff for their invaluable feedback on this matter. He will post a reply here as soon as possible and provide a code snippet if possible.

    Take care and have a nice day :slight_smile:

    Cheers, Sajid

  • Sajid

    Hi @Nanjo

    Hope you are doing good today :slight_smile:

    I come up with following code that will map the custom fields of parent recurring even with child instances.

    function wpmu_map_custom_meta_data_of_child_with_parent($post_ID){
    
    	$parent_ID =  get_post( $post_ID )->post_parent;
    
    	$customfields = array('custom_field_1', 'custom_field_2');
    
    	if($customfields) :
    		foreach ($customfields as $meta_key) {
    
    			$parent_meta_value = get_post_meta($parent_ID, $meta_key, true);
    			update_post_meta($post_ID, $meta_key, $parent_meta_value);
    		}
    	endif;
    }
    add_action('eab-events-recurrent_event_child-save_meta','wpmu_map_custom_meta_data_of_child_with_parent', 10);

    I tested it on my own site and it worked for me.

    The only thing you have to change is the meta key names in "$customfields" array. Replace the existing dummy keys with your own custom field name (see screenshots for field names). You can add multiple names of fields separated by coma.

    After making changes simply add it in functions.php file of your child theme or use as mu-plugin.

    Take care and have a nice day :slight_smile:

    Cheers, Sajid

  • Nanjo

    Thank you, Sajid. That action hook 'eab-events-recurrent_event_child-save_meta' is useful.
    With your help, my solution looks something like this for the one ACF field I wanted:

    function xxx_spawn_populate( $post_id ) {
        if (!empty($parent = get_post( $post_id )->post_parent)):
            $value= get_field('field_xxxxxkeyxxxxx',$parent); // key
            update_field( 'field_xxxxxkeyxxxxx', $value, $post_id );
        endif;
    }
    add_action ( 'eab-events-recurrent_event_child-save_meta', 'xxx_spawn_populate');

    It uses ACF's update_field() function.

    It should be noted that you can do a foreach loop through all ACF fields of the parent using ACF's get_fields() function.

  • Sajid

    Hi @Nanjo

    Hope you are doing good today :slight_smile:

    I am glad to know it worked for you. I know ACF have its own functions to get/update fields. But they said it can also be used with default wp functions like get_post_meta and update_post_meta.

    So that's why I used the native functions beside making the script dependant to ACF. Now, any one can use above code snippet even if they are not using ACF plugin and looking for same solution.

    Again, I am glad to know I could help, feel free to start new thread if you need further assistance.

    Thanks for the tip and updated code, much appreciated :slight_smile:

    Take care and have a nice day :slight_smile:

    Cheers, Sajid