I've written a PHP script to create custom posts programmatically from Google Calendar events, and an action hook that purges all existing events and repopulates it with current ones. This works fine when I click the button I've added to the views. However, when I try to excecute the hook as a cron job (using the WP Control plugin) it purges existing posts, but it does not create new ones. The only output I get from debugging is a warning about contextual_help being deprecated, which I don't think is relevant for this – but I could be wrong, of course.

Here's (the relevant parts of) the code:

 function create_event_posts() {
 //calendar-ids and parameters are stored in variables outside of the function for reference in other functions
 global $calendarIds;
 global $optParams;
 
 //delete existing posts <-- This works fine! :)
 $allEvents = get_posts(array('post_type' => 'event', 'numberposts' => -1));
 foreach ($allEvents as $eachEvent) {
 wp_delete_post($eachEvent -> ID, true);
 }
 
 //call to google calendar script
 require_once ABSPATH . '/path/to/vendor/autoload.php';
 $client = new Google_Client();
 $client -> setApplicationName('customCalendar');
 $client -> setAuthConfig(ABSPATH . '/path/to/credentials.json');
 $client = getClient();
 $service = new Google_Service_Calendar($client);
 
 //collecting all the events
 foreach ($calendarIds as $calendarId) {
 $results = $service -> events -> listEvents($calendarId, $optParams);
 $events[] = $results -> getItems();
 }
 $events = flatten($events);
 
 foreach ($events as $event) {
 $eventContent = $eventTitle = $eventColor = $calendarTitle = '';
 $calendarTitle = $event -> organizer -> displayName;
 $eventContent = apply_filters('the_content', $event -> getDescription());
 $start = $event -> start -> dateTime;
 $end = $event -> end -> dateTime;
 $fmt = new \IntlDateFormatter('nb-NO', NULL, NULL);
 $fmt -> setPattern('EEEE d. MMMM');
 $outputDate = $fmt -> format(strtotime($start));
 $outputStart = date('H:i', strtotime($start));
 $outputEnd = date('H:i', strtotime($end));
 
 if (empty($start)) {
 $start = $event -> start -> date;
 }
 
 $eventPost = array(
 'post_author' => 1,
 'post_date' => date('Y-m-d H:i:s'),
 'post_content' => $eventContent,
 'post_title' => $eventTitle,
 'post_excerpt' => $outputDate . ', kl. ' . $outputStart . ' – ' . $outputEnd,
 'post_status' => 'publish',
 'post_type' => 'event',
 'post_category' => array($eventCategory), //I cut out the part where this variable is defined, to save space
 'meta_input' => array(
 '_EventStartDate' => $start,
 '_Room' => $calendarTitle,
 ),
 );
 if (!post_exists($eventTitle, $eventContent, '', 'event')) {
 $post_ID = wp_insert_post($eventPost);
 }
 }
 }
And the hook is:

 add_action('admin_post_create_event_posts', 'create_event_posts');