WordPress

php – Problem z automatycznie tworzonymi wydarzeniami, które nie są wyświetlane w interfejsie użytkownika

  • 16 lipca, 2024
  • 3 min read
php – Problem z automatycznie tworzonymi wydarzeniami, które nie są wyświetlane w interfejsie użytkownika


Napotykam problem z wydarzeniami utworzonymi programowo za pomocą funkcji tribe_create_event wtyczki The Events Calendar. Oto szczegóły problemu:

Proces:

Post WordPress jest planowany lub publikowany za pośrednictwem zautomatyzowanego procesu (np. WP Automatic). Wydarzenie jest tworzone programowo przy użyciu funkcji tribe_create_event na podstawie opublikowanego posta. Wydarzenie obejmuje szczegóły, takie jak tytuł, treść, data rozpoczęcia, data zakończenia, kategorie, organizator i lokalizacja. Chociaż wydarzenie jest pomyślnie zapisywane w bazie danych i wszystkie metadane wydają się być poprawnie ustawione, wydarzenie nie wyświetla się natychmiast na froncie. Aby wydarzenie było widoczne na froncie, muszę ręcznie otworzyć wydarzenie w panelu administracyjnym WordPress i je zaktualizować. Mój kod:

add_action(’save_post’, 'create_event_on_scheduled_publish’, 10, 3); function create_event_on_scheduled_publish($post_ID, $post, $update) { // Sprawdzanie, czy post jest już przetwarzany, aby zapobiec duplikowaniu zdarzeń

jeśli (get_post_meta($post_ID, 'event_created’, true) === true) { return; }

if ($post->post_type == 'post’) { //$post->post_status == 'future’ && // Pobierz identyfikator, tytuł i treść wpisu $ID = $post->ID; $title = get_the_title($ID); $content = apply_filters(’the_content’, $post->post_content);

// Extract start and end dates from the content using regex
preg_match_all('/\b\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\b/', $content, $matches);

if (count($matches[0]) >= 1) {
    $start_date = $matches[0][0];
    $end_date = isset($matches[0][1]) ? $matches[0][1] : '';

    // Retrieve post categories by name
    $post_categories = wp_get_post_categories($ID);
    $event_categories = array();

    foreach ($post_categories as $c) {
        $cat = get_category($c);
        if ($cat) {
            // Get the category by name
            $event_cat = get_term_by('name', $cat->name, 'tribe_events_cat');
            if ($event_cat) {
                $event_categories[] = $event_cat->term_id;
            }
        }
    }

    // Create event post
    // $event = array(
    //     'post_title'    => $title,
    //     'post_content'  => $content,
    //     'post_status'   => 'publish',
    //     'post_type'     => 'tribe_events',
    //     'post_author'   => 1, // Ensure a valid author ID is set
    //     // 'tax_input'     => array(
    //     //     // 'tribe_events_cat' => $event_categories,
    //     // ),
    // );
    $event_args = array(
            'post_title' => $title,
            'post_content' => $content,
            'post_status' => 'publish',
            'EventStartDate' => $start_date,
            'EventEndDate' => $end_date,
        );

        // Insert the event
        $event_id = tribe_create_event($event_args);

    // Insert the event post into the database
    // $event_id = wp_insert_post($event);

    // Assign categories to the event
    if (!empty($event_categories)) {
        wp_set_object_terms($event_id, $event_categories, 'tribe_events_cat');
    }
    update_post_meta($post_ID, 'event_created', true); // to avoid duplication

    // Set event start and end dates
    if ($event_id) {
        update_post_meta($event_id, '_EventStartDate', $start_date);
        if (!empty($end_date)) {
            update_post_meta($event_id, '_EventEndDate', $end_date);
        }

        // Set organizer (whoisin)
        $organizer = get_term_by('name', 'whoisin', 'tribe_organizer');
        if ($organizer) {
            update_post_meta($event_id, '_EventOrganizerID', $organizer->term_id);
        }

        // Set location (barcelona)
        $location = get_term_by('name', 'barcelona', 'tribe_venue');
        if ($location) {
            update_post_meta($event_id, '_EventVenueID', $location->term_id);
        }

        // Set map display options
        update_post_meta($event_id, '_EventShowMap', '1');
        update_post_meta($event_id, '_EventShowMapLink', '1');

            // Clear the cache (if a caching plugin is used)
            if (function_exists('wp_cache_clear_cache')) {
                wp_cache_clear_cache();
            }

            // Flush rewrite rules
            flush_rewrite_rules();


    }
} else {

    // Log error or handle the case when dates are not found
    error_log("Start and/or end date not found in post content for post ID: $ID");
}

}


Źródło

Warto przeczytać!  Logowanie do WordPressa ustawia plik cookie, który przetrwa wyjście przeglądarki (funkcja wp_signon)