Skip to content

Instantly share code, notes, and snippets.

@philipdowner
Last active February 11, 2025 19:17
Show Gist options
  • Save philipdowner/1782982c2d2799ca33371435f0247d10 to your computer and use it in GitHub Desktop.
Save philipdowner/1782982c2d2799ca33371435f0247d10 to your computer and use it in GitHub Desktop.
Programatically updating Tribe Events Calendar data using ORM
<?php
// Uses the symfony/var_dumper package for the dump() function
/** First we create the Event */
$created = tribe_events()
->set_args([
'title' => "Test Event",
'start_date' => "2025-02-14 08:00:00",
'end_date' => "2025-02-14 10:00:00",
'laws_external_id' => 123,
'status' => 'publish'
])
->create();
$postId = $created->ID;
dump($postId); //6430
/**
* We now query by the assigned meta key. An occurrence is created and a provisional ID is returned.
* The provisional ID does not yet exist in the wp_tec_occurrences table.
*/
$event = tribe_events()
->where('meta_query', [
[
'key' => 'laws_external_id',
'value'=> 123,
'compare' => "=",
'type' => 'NUMERIC'
]
])
;
$provisionalEventId = $event->first()->ID;
dump($provisionalEventId); //10000119
dump($event->first()->start_date); //"2025-02-14 08:00:00"
dump(get_post_meta($provisionalEventId)); // [... '_EventStartDate' => [0 => "2025-02-14 08:00:00"]]
/**
* Now we update the event using the Provisional ID, adjusting the start_date by +30m
* The Provisional ID is now persisted in the wp_tec_occurrences table.
*
* The WP Admin lists 2 events in the UI, but both have the same ID matching $postId - /wp-admin/edit.php?post_type=tribe_events
* Both posts display a start_date of "2025-02-14 08:00:00" in the WP UI
*/
$updated = tribe_events()->where('ID', $provisionalEventId)
->set('start_date', "2025-02-14 08:30:00")
->save();
dump($updated); // [10000119 => true]
/**
* When accessed via the provisional ID the post_meta that is returned appears to not be updated
**/
dump(get_post_meta($provisionalEventId)); // [... '_EventStartDate' => [0 => "2025-02-14 08:00:00"]]
/**
* However, when we get post_meta for the original post ID, we can see that it's been persisted
*/
dump(get_post_meta($postId)); //[... '_EventStartDate' => [0 => "2025-02-14 08:00:00", 1 => "2025-02-14 08:30:00"]]
/**
* We can query for the Event again, and see that the output start_date is incorrect
*/
dump(tribe_events()->where('ID', $provisionalEventId)->first()->start_date);
/**
* Querying by the original post ID also fails.
**/
dump(tribe_events()->where('ID', $postId)->first()->start_date);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment