Title: using wp_set_object with CPT onony and Events manager
Last modified: August 31, 2016

---

# using wp_set_object with CPT onony and Events manager

 *  [efrap](https://wordpress.org/support/users/efrap/)
 * (@efrap)
 * [10 years, 2 months ago](https://wordpress.org/support/topic/wp_set_object-with/)
 * I am using CPT onomies and Events Manager. Trying to save from front end events
   form.
 * $cpt_onomy->wp_set_object_terms…
 * The Event Manager hook comes in to early for CPT onomies. It does save when using
   cascading callbacks (wp_loaded inside em_event_save) – but this prevents other
   EM save functions from firing.
 * [http://wordpress.org/plugins/cpt-onomies/](http://wordpress.org/plugins/cpt-onomies/)
 * My function here
 *     ```
       function first_callback(){
        if( !is_admin() ){
       	function add_org(){
       		global $cpt_onomy;
       		global $EM_Event;
       		$cpt_onomy->wp_set_object_terms($EM_Event->ID,(array)$_POST['tax_input']['orgs'],'orgs');
       		}
       add_action('wp_loaded','add_org');
        }
       }
       add_filter( 'em_event_save', 'first_callback',10,2 );
       ```
   
 * Function structure that works for regular custom taxnomies
 *     ```
       function enregistrer_tax($result,$EM_Event){
         if( $result ){
       	 if( !is_admin() ){
       		$types = intval($_POST['types']);
       		wp_set_object_terms($EM_Event->ID,$types,'types');
       	 }
       	}
       	    return $result;
       }
       add_action( 'em_event_save', 'enregistrer_tax',9,2 );
       ```
   

Viewing 1 replies (of 1 total)

 *  Thread Starter [efrap](https://wordpress.org/support/users/efrap/)
 * (@efrap)
 * [10 years, 2 months ago](https://wordpress.org/support/topic/wp_set_object-with/#post-7206360)
 * The solution is to handle post meta without cpt-onomies. In the frontend EM event
   form, there are two CPT-onomies, one dropdown and one with chexboxes. By comparing
   form selection to post CPT-onomies post-meta, it is possible to add and remove.
 *     ```
       function save_onomies($result,$EM_Event){
       	if( $result ){
       		if( !is_admin() ){
       			$single = intval($_POST['taxa']);
       			$multiple = array_map('intval', (array)$_POST['tax_input']['taxb']);
       			$multiple[] = $single; 	// add single onomy to array
       			$onomies = get_post_meta ( $EM_Event->ID, CPT_ONOMIES_POSTMETA_KEY );
       			$toadds = array_diff( $multiple, $onomies );
       			if ( $toadds ) {
       				foreach( $toadds as $toadd ) {
       				add_post_meta( $EM_Event->ID, CPT_ONOMIES_POSTMETA_KEY, $toadd, false);
       				}
       			}
       			$todeletes = array_diff( $onomies, $multiple );
       			if ( $todeletes ) {
       				foreach( $todeletes as $todelete ) {
       				delete_post_meta($EM_Event->ID, CPT_ONOMIES_POSTMETA_KEY, $todelete );
       				}
       			}
       		}
       	}
       	return $result;
       }
   
       add_action( 'em_event_save', 'save_onomies',9,2 );
       ```
   

Viewing 1 replies (of 1 total)

The topic ‘using wp_set_object with CPT onony and Events manager’ is closed to new
replies.

 * 1 reply
 * 1 participant
 * Last reply from: [efrap](https://wordpress.org/support/users/efrap/)
 * Last activity: [10 years, 2 months ago](https://wordpress.org/support/topic/wp_set_object-with/#post-7206360)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
