Title: Facebook events importing: custom code
Last modified: August 31, 2016

---

# Facebook events importing: custom code

 *  Resolved [fersamp](https://wordpress.org/support/users/fersamp/)
 * (@fersamp)
 * [10 years, 1 month ago](https://wordpress.org/support/topic/facebook-events-importing-custom-code/)
 * Hi,
    It’s been a long time I’d like to implement events from facebook to EM: 
   I tried a lot of plugins but none solve my problem completely.
 * So I decided to implement it manually:
    I used facebook SDK and I can retrieve
   what I need from my facebook page. Great.
 * But I don’t succeed to insert in EM the events I got from facebook.
 * Here my code in my cycle where I loop facebooks events:
 *     ```
       //check if event it's already been imported
       $args = array (
       'post_type' => 'event',
       'posts_per_page' => -1,
       'meta_key' => '_facebook_event_id',
       'meta_query' => array(
       'key'		=> '_facebook_event_id',
       'value'		=> $event_id,
       ),
       );
       $loop = new WP_Query( $args );
   
       //if presents, I update data
       if( $loop->have_posts() ){
       	// se l'evento  presente aggiorno i suoi dati
       	//$u++;
       	while ( $loop->have_posts() ) : $loop->the_post();
   
       	$post_id = get_the_ID();
       	$post_information = array(
       	'post_type' => 'event',
       	'ID' => $post_id,
       	'post_title' => wp_strip_all_tags($e->name),
       	'post_content' => wp_strip_all_tags($e->description),
       	);
   
       	wp_update_post( $post_information );
       	endwhile;
       }else{ 
   
       	// else I add event
       	$post_information = array(
       	'post_type' => 'event',
       	'post_title' => wp_strip_all_tags($e->name),
       	'post_content' => wp_strip_all_tags($e->description),
       	'post_status' => 'publish',
       	);		
   
       	// insert event post in WP
       	$post_id = wp_insert_post( $post_information ); 
   
       	// insert event in events table
       	$wpdb->insert(
       	'SqE_em_events',
       	array(
       	'event_owner' => 1,
       	'event_status' => 1,
       	'event_name' =>  wp_strip_all_tags($e->name),
       	'event_start_time' => $starTime,
       	'event_end_time' => $endTime,
       	'event_start_date' => $startDate,
       	'event_end_date' => $endDate,
       	'post_content' => wp_strip_all_tags($e->description),
       	'event_rsvp' => 0,
       	'post_id' => $post_id,
       	'location_id' => $location_id
       	),
       	array(
       	'%s',
       	'%s',
       	'%s',
       	'%s',
       	'%s',
       	'%s',
       	'%s',
       	'%s',
       	'%s',
       	'%d',
       	'%d'
       	)
       	);
   
       }
   
       // update meta for event
       add_post_meta($post_id, '_facebook_event_id', $eId, true);
       update_post_meta($post_id,'_event_start_time',$starTime);
       update_post_meta($post_id,'_event_end_time',$endTime);
       update_post_meta($post_id,'_event_start_date',$startDate);
       update_post_meta($post_id,'_event_end_date',$endDate);
       update_post_meta($post_id,'_event_status', 1);
       update_post_meta($post_id,'_location_status', 1);
       ```
   
 * Can someone help me to insert manually and safely an event into EM?
 * **PS: If I use this, Have I to add some includes in php file?
    This doesn’t work
   for me.
 *     ```
       $EM_Event = new EM_Event();
       $EM_Event->event_name = $evt_name;
       $EM_Event->event_start_date = $evt_start_d;
       $EM_Event->event_start_time = $evt_start_t;
       $EM_Event->event_end_date = $evt_end_d;
       $EM_Event->event_end_time = $evt_end_t;
       $EM_Event->start = strtotime($EM_Event->event_start_date." ".$EM_Event->event_start_time);
       $EM_Event->end = strtotime($EM_Event->event_end_date." ".$EM_Event->event_end_time);
       $EM_Event->event_all_day = $evt_all_day;
       $EM_Event->save();
       ```
   
 * Really thanks guys

Viewing 14 replies - 1 through 14 (of 14 total)

 *  [caimin_nwl](https://wordpress.org/support/users/caimin_nwl/)
 * (@caimin_nwl)
 * [10 years, 1 month ago](https://wordpress.org/support/topic/facebook-events-importing-custom-code/#post-7237381)
 * At what point and where are you running this code?
 *  Thread Starter [fersamp](https://wordpress.org/support/users/fersamp/)
 * (@fersamp)
 * [10 years, 1 month ago](https://wordpress.org/support/topic/facebook-events-importing-custom-code/#post-7237418)
 * I’m using it in a shortcode in custom function.php…
    I don’t think is invocation
   problem…Have I to add some include to use new EM_Event()?
 *  Thread Starter [fersamp](https://wordpress.org/support/users/fersamp/)
 * (@fersamp)
 * [10 years, 1 month ago](https://wordpress.org/support/topic/facebook-events-importing-custom-code/#post-7237431)
 * I tried with query but nothing…
 * Events data are correct to 100%.
 * How can I add a new event to EM manually?
 *  [caimin_nwl](https://wordpress.org/support/users/caimin_nwl/)
 * (@caimin_nwl)
 * [10 years, 1 month ago](https://wordpress.org/support/topic/facebook-events-importing-custom-code/#post-7237482)
 * Have you tried adding the event using the EM_Event() code you pasted? That should
   work, provided you use the correct data with it.
 *  Thread Starter [fersamp](https://wordpress.org/support/users/fersamp/)
 * (@fersamp)
 * [10 years, 1 month ago](https://wordpress.org/support/topic/facebook-events-importing-custom-code/#post-7237500)
 * Hi caimin_nwl,
    I tried with this
 *     ```
       $EM_Event = new EM_Event();
       $EM_Event->event_name = $e->name;
       $EM_Event->event_start_date = $_event_start_date;
       $EM_Event->event_start_time = $_event_start_time;
       $EM_Event->event_end_date = $_event_end_date;
       $EM_Event->event_end_time = $_event_end_time;
       _log('FRA: aggiunta evento: ' . $e->name . ' startdate ' . $_event_start_date . ' enddate ' . $_event_end_date );
   
       $EM_Event->event_all_day = 0;
       $EM_Event->save();
       ```
   
 * but event is not added.
    Log says correct data: name, startdate and enddate like‘
   2016-04-07’.
 * It’s a lot of days I’m fighting with this problem 🙁
 * Is there someone that succeded to add an event with $EM_Event = new EM_Event();?
   
   Have I to add an import to use that method?
 *  Thread Starter [fersamp](https://wordpress.org/support/users/fersamp/)
 * (@fersamp)
 * [10 years, 1 month ago](https://wordpress.org/support/topic/facebook-events-importing-custom-code/#post-7237504)
 * **Update: I tested that code again and again: no events added in em_events, posts
   or postmeta table.**
 * I studied a lot the EM process event creation and I think these are tables implicated:
 * 1) **TABLE posts**, creates event and location row:
    – the event is created as
   post type = ‘event’. – the base information like title and content are set – 
   status is set as ‘publish’
 * – the location is created as post type = ‘location’.
    – the base information 
   like title and content are set – status is set as ‘publish’
 * 2) **TABLE postmeta**, creates event and location row meta:
    – all event meta
   like _event_id, _event_id, _location_id etc… -all location meta like _location_address,
   _location_town,_location_state etc…
 * 3) **TABLE em_locations**, location is inserted with post_id, location_name ecc.
   
   post_id is the id of post type
 * 4) **TABLE em_events**, event is inserted with name, location address, stardate
   ecc.
    I suppose info data event are obtained from metapost table.
 * I tried to replace these flow with queries, but in vain…
 *  Thread Starter [fersamp](https://wordpress.org/support/users/fersamp/)
 * (@fersamp)
 * [10 years, 1 month ago](https://wordpress.org/support/topic/facebook-events-importing-custom-code/#post-7237513)
 * **update:**
 * I succeded to add manually rows to correct tables:
 * I added event and location post in post table, event in event table, location
   in location table and correct postmeta data in postmeta table…
    **Everything 
   seems correct, but I still not see new event in events list in backend.** Here
   my code, in the events cycle for to add:
 *     ```
       $args = array(
       'post_type' => 'event',
       'meta_query' => array(
       array(
       'key' => '_facebook_id',
       'value' => $event_id,
       'compare' => '=')
       )
       );
   
       $check = get_posts($args);
   
       if (!empty($check)) {
       	continue;
       }else{
   
       	$_event_start_date = date('Y-m-d', strtotime($e->start_time));
       	$_event_end_date = date('Y-m-d', strtotime($e->end_time) + 3600);
       	$_event_start_time = date('H:i:s', strtotime($e->start_time));
       	$_event_end_time = date('H:i:s', strtotime($e->end_time) + 3600);
   
       	$id = wp_insert_post(array(
       	'post_type' => 'event',
       	'post_title' => wp_strip_all_tags($e->name),
       	'post_author' => '1',
       	'post_content' => wp_strip_all_tags($e->description),
       	'post_status' => 'publish',
       	));
   
       	global $wpdb;
       	$query = 'select * from ' . $wpdb->prefix . 'em_locations where location_name like "%' . $wpdb->esc_like($locationName) . '%"';
   
       	$location_row = $wpdb->get_row($query);
   
       	if ($location_row) {
   
       		$location_id = $location_row->location_id;
   
       	} else {
   
       		$post_id = wp_insert_post(array(
       		'post_type' => 'location',
       		'post_title' => $locationName,
       		'post_status' => 'publish',
       		));
   
       		update_post_meta($post_id, '_location_town', 'Roma');
       		update_post_meta($post_id, '_location_country', 'Italia');
       		update_post_meta($post_id, '_location_address', $locationName);
       		update_post_meta($post_id, '_location_state', 'Italia');
   
       		$queryLocation = 'insert into ' . $wpdb->prefix . 'em_locations '
       		. 'set post_id="' . $wpdb->escape($post_id) . '",'
       		. 'location_name="' . $wpdb->escape($locationName) . '",'
       		. 'location_address="' . $wpdb->escape($locationName) . '",'
       		. 'location_town="Roma",'
       		. 'location_state="Italia",'
       		. 'location_status="1",'
       		. 'location_country="Italia",'
       		. 'blog_id="0",'
       		. 'location_slug="' . $wpdb->escape(sanitize_title($locationName)) . '",'
       		. 'location_latitude="' . $wpdb->escape(sanitize_title($lat)) . '",'
       		. 'location_longitude="' . $wpdb->escape(sanitize_title($long)) . '",'
       		. 'location_owner=1';
   
       		$wpdb->query($queryLocation);
   
       		$location_id = $wpdb->insert_id;
       		if($location_id){
   
       			update_post_meta($post_id, '_location_status', 1);
       			update_post_meta($post_id, '_location_longitude', $long);
       			update_post_meta($post_id, '_location_latitude', $lat);
       			update_post_meta($post_id, '_location_country', 'Italia');
       			update_post_meta($id, '_location_id', $location_id);
       		}
   
       	}
   
       	$sqlEvent = "INSERT INTO " . $wpdb->prefix . "em_events (event_owner, event_status, event_name, event_start_time, event_end_time, event_start_date, event_end_date, post_content, event_rsvp, post_id, location_id)
       				values (
       				1,
       				1,
       				'" . wp_strip_all_tags($e->name) . "' ,
       				'" . $_event_start_time . "' ,
       				'" . $_event_end_time . "' ,
       				'" . $_event_start_date . "' ,
       				'" . $_event_end_date . "' ,
       				'" . wp_strip_all_tags($e->description) . "',
       				0,
       				'" . $id . "',
       				'" . $location_id . "'
       				)";
   
       	$wpdb->query($sqlEvent);
   
       	$event = new EM_Event($wpdb->insert_id);
       	$event->set_status(1, true);
       	$result = $event->save();
   
       	update_post_meta($id, '_event_end_date', $_event_end_date);
       	update_post_meta($id, '_event_end_time', $_event_end_time);
       	update_post_meta($id, '_event_start_date', $_event_start_date);
       	update_post_meta($id, '_event_start_time', $_event_start_time);
       	update_post_meta($id, '_location_id', $location_id);
       	update_post_meta($id, '_facebook_id', $event_id);
       ```
   
 * I’m not sure about last part:
 * **$event = new EM_Event($wpdb->insert_id);
    $event->set_status(1, true); $result
   = $event->save();
 * **WHAT I HAVE TO DO MORE? 🙁**
 *  [caimin_nwl](https://wordpress.org/support/users/caimin_nwl/)
 * (@caimin_nwl)
 * [10 years, 1 month ago](https://wordpress.org/support/topic/facebook-events-importing-custom-code/#post-7237516)
 * Have you tried adding
 * `global $EM_Event;`
 * before using
 * `$event = new EM_Event($wpdb->insert_id);` ?
 *  Thread Starter [fersamp](https://wordpress.org/support/users/fersamp/)
 * (@fersamp)
 * [10 years, 1 month ago](https://wordpress.org/support/topic/facebook-events-importing-custom-code/#post-7237524)
 * I added `global $EM_Event;`, nothing changed.
 * Insert are correct:
    1) post event is added into posts table; 2) location event
   is added into posts table; 2) location event is added in em_locations; 4) event
   is added in EM table; 3) post and location meta are added;
 * After these inserts, I don’t see new event in backend, but seems all data are
   inserted in db.
 * How does they work `$event = new EM_Event($wpdb->insert_id);`
    and `save()` ?
   I think I miss something…
 *  [Robswaimea](https://wordpress.org/support/users/robswaimea/)
 * (@robswaimea)
 * [10 years, 1 month ago](https://wordpress.org/support/topic/facebook-events-importing-custom-code/#post-7237525)
 * Hey Fersamp.. wishing you good luck on this project.
 * I think you have looked at this pastebin…
    [http://pastebin.com/3UDjZ8Le](http://pastebin.com/3UDjZ8Le)
   There is a little bit of code in it you might paste and see what it produces 
   when you run your php file…
 *     ```
       mysql_query($sql);
       echo mysql_error();
       ```
   
 * Rob
 *  Thread Starter [fersamp](https://wordpress.org/support/users/fersamp/)
 * (@fersamp)
 * [10 years, 1 month ago](https://wordpress.org/support/topic/facebook-events-importing-custom-code/#post-7237528)
 * Hi Rob,
    thank you for help, but I think that inserts are correct because I see
   the rows that are inserted.
 * Instead, I saw that in
 *     ```
       $event = new EM_Event($wpdb->insert_id);
       $event->set_status(1, true);
       $result = $event->save();
       ```
   
 * $event->save() returns false, even if $wpdb->insert_id is the correct event id
   just inserted in em_events.
    I don’t understand why….
 *  Thread Starter [fersamp](https://wordpress.org/support/users/fersamp/)
 * (@fersamp)
 * [10 years, 1 month ago](https://wordpress.org/support/topic/facebook-events-importing-custom-code/#post-7237538)
 * Update:
    Do `$event->save()` has to return true, isn’t true?
 *  [ronfedele](https://wordpress.org/support/users/ronfedele/)
 * (@ronfedele)
 * [10 years, 1 month ago](https://wordpress.org/support/topic/facebook-events-importing-custom-code/#post-7237562)
 * Take a look at this code. I know it is a different system but a lot of similarities.
 * [https://wordpress.org/plugins/events-made-easy/](https://wordpress.org/plugins/events-made-easy/)
 * Ron
 *  Thread Starter [fersamp](https://wordpress.org/support/users/fersamp/)
 * (@fersamp)
 * [10 years, 1 month ago](https://wordpress.org/support/topic/facebook-events-importing-custom-code/#post-7237564)
 * Thank you Ron, you are very kind, but I made all my site according to Events 
   Manager, so I can’t change plugin.

Viewing 14 replies - 1 through 14 (of 14 total)

The topic ‘Facebook events importing: custom code’ is closed to new replies.

 * ![](https://ps.w.org/events-manager/assets/icon-256x256.png?rev=1039078)
 * [Events Manager - Calendar, Bookings, Tickets, and more!](https://wordpress.org/plugins/events-manager/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/events-manager/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/events-manager/)
 * [Active Topics](https://wordpress.org/support/plugin/events-manager/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/events-manager/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/events-manager/reviews/)

 * 14 replies
 * 4 participants
 * Last reply from: [fersamp](https://wordpress.org/support/users/fersamp/)
 * Last activity: [10 years, 1 month ago](https://wordpress.org/support/topic/facebook-events-importing-custom-code/#post-7237564)
 * Status: resolved