Forum Replies Created

Viewing 1 replies (of 1 total)
  • Hello @shingen0810 ! @buyyes colleague here. Upon examining the database tables, I found that programmatically created events were being added to the ‘wp_posts’ table, but not to the Event Calendar’s specific tables, ‘wp_tec_events’ and ‘wp_tec_occurrences’. To resolve this, I made a function that manually fills them. The following code is triggered every time a ‘tribe_events’ post is published:

    add_action('publish_tribe_events', 'estrcf_eventFix', 10, 2);

    function estrcf_eventFix ($post_id, $post)
    {
    remove_action('publish_tribe_events', 'estrcf_eventFix', 10);

    if (!wp_is_post_autosave($post_id) && !wp_is_post_revision($post_id)) {
    estrcf_createEvent($post_id);
    }

    add_action('publish_tribe_events', 'estrcf_eventFix', 10, 2);
    }

    function estrcf_createEvent($post_id)
    {
    global $wpdb;
    $wpdb->show_errors();
    $post = get_post($post_id);

    if ($post) {
    $timezone = get_option('timezone_string');
    $date_format = 'Y-m-d H:i:s';
    $start_date = get_post_meta($post->ID, '_EventStartDate', true);
    $end_date = get_post_meta($post->ID, '_EventEndDate', true);
    $start_date_utc = get_gmt_from_date($start_date, $date_format);
    $end_date_utc = get_gmt_from_date($end_date, $date_format);

    $start_datetime = new DateTime($start_date);
    $end_datetime = new DateTime($end_date);
    $interval = $start_datetime->diff($end_datetime);
    $duration = $interval->s + $interval->i * 60 + $interval->h * 3600 + $interval->d * 86400 + $interval->m * 2629746 + $interval->y * 31556952;

    $post_array = array(
    'post_id' => $post->ID,
    'start_date' => $start_date,
    'end_date' => $end_date,
    'start_date_utc' => $start_date_utc,
    'end_date_utc' => $end_date_utc,
    'duration' => $duration,
    'timezone' => $timezone
    );

    $wpdb->insert('wp_tec_events', $post_array);

    array_pop($post_array);

    $query = $wpdb->prepare("SELECT event_id FROM wp_tec_events WHERE post_id = %d", $post_id);
    $post_array['event_id'] = $wpdb->get_var($query);

    $post_array['hash'] = md5($post_array['event_id']. $start_date . $end_date . $timezone);

    echo "<pre>";
    print_r($interval);
    print_r($post_array);
    echo "</pre>";

    $wpdb->insert('wp_tec_occurrences', $post_array);

    } else {
    echo 'Post not found.';
    }
    }

    Note: some automation plugins firstly publish post and then add custom fields or other data to it. You need to make sure that at least _EventStartDate and _EventEndDate are correctly filled in at the time of post recieving the status ‘publish’.

    While the current solution works, it’s not ideal because of:

    $post_array[‘duration’]: The start and end dates are taken from the custom fields ‘_EventStartDate’ and ‘_EventEndDate’ (used by Events Calendar), However, the calculated duration of the event might not be exactly accurate compared to those calculated by the Events Calendar. I believe this may be due to the number of seconds I used to multiply months and years.

    $post_array[‘hash’]: I don’t know how Events Calendar usually calculates hash for events/occurrences so I used event_id, start_date, end_date, and timezone for it. I don’t know whether this may cause problems in the future or not.

    And for deletion of the event you can use something like this:

    add_action('before_delete_post', 'estrcf_deleteEvent', 10, 2);

    function estrcf_deleteEvent($post_id, $post)
    {
    if (!($post->post_type == 'tribe_events')) {
    return;
    }
    global $wpdb;

    $where = array('post_id' => $post_id);
    $result = $wpdb->delete(
    'wp_tec_events',
    $where
    );

    if ($result !== false) {
    echo "The row was deleted from wp_tec_events successfully.";
    } else {
    echo "No row was found in wp_tec_events with the specified post_id value.";
    }

    $result = $wpdb->delete(
    'wp_tec_occurrences',
    $where
    );

    if ($result !== false) {
    echo "The row was deleted from wp_tec_occurrences successfully.";
    } else {
    echo "No row was found in wp_tec_occurrences with the specified post_id value.";
    }
    }

    I hope this was helpful.

Viewing 1 replies (of 1 total)