Created
January 11, 2024 05:28
-
-
Save chikaibeneme/b60591972ac6248b1571c615ed52b4f9 to your computer and use it in GitHub Desktop.
To add a category to all events using The Events Calendar in WordPress
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 category to all events | |
| function add_category_to_all_events() { | |
| // Get all events | |
| $events = get_posts(array( | |
| 'post_type' => 'tribe_events', | |
| 'posts_per_page' => -1, // Get all events | |
| )); | |
| // Check if events are found | |
| if ($events) { | |
| foreach ($events as $event) { | |
| // Get current event categories | |
| $categories = wp_get_post_terms($event->ID, 'tribe_events_cat', array('fields' => 'ids')); | |
| // Add your category ID to the event's categories (replace 'your_category_id' with the actual category ID) | |
| $categories[] = 'your_category_id'; | |
| // Update the event with the new categories | |
| wp_set_post_terms($event->ID, $categories, 'tribe_events_cat'); | |
| } | |
| } | |
| } | |
| // Hook the function to run on activation (you can remove this line after running it once) | |
| add_action('init', 'add_category_to_all_events'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment