Created
February 17, 2025 20:36
-
-
Save chikaibeneme/176fed6163451db6795665460a7e3e6d to your computer and use it in GitHub Desktop.
Event Featured image to product thumbnail.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| add_action( 'save_post_tribe_events', function ( $post_id, $post, $update ) { | |
| // Ensure this is not an autosave | |
| if ( wp_is_post_autosave( $post_id ) || wp_is_post_revision( $post_id ) ) { | |
| return; | |
| } | |
| // Get the event's featured image | |
| $event_thumbnail_id = get_post_thumbnail_id( $post_id ); | |
| error_log('Event ID: ' . $post_id . ' - Thumbnail ID: ' . $event_thumbnail_id); | |
| // Check if a featured image exists | |
| if ( !$event_thumbnail_id ) { | |
| error_log('No featured image found for event.'); | |
| return; | |
| } | |
| // Get all tickets linked to this event | |
| $tickets = Tribe__Tickets__Tickets::get_all_event_tickets( $post_id ); | |
| foreach ( $tickets as $ticket ) { | |
| // Ensure it's a WooCommerce ticket | |
| if ( $ticket->provider_class !== 'Tribe__Tickets_Plus__Commerce__WooCommerce__Main' ) { | |
| continue; | |
| } | |
| // If the ticket product has no image, set the event's image | |
| if ( ! has_post_thumbnail( $ticket->ID ) ) { | |
| set_post_thumbnail( $ticket->ID, $event_thumbnail_id ); | |
| // Force WooCommerce to recognize the change | |
| wp_update_post( array( | |
| 'ID' => $ticket->ID, | |
| 'post_status' => get_post_status( $ticket->ID ) | |
| )); | |
| error_log('Set featured image for product ID: ' . $ticket->ID); | |
| } else { | |
| error_log('Product ID: ' . $ticket->ID . ' already has an image.'); | |
| } | |
| } | |
| }, 10, 3 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment