Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save chikaibeneme/6ec79ba0d608997b6214c9ef0abb4b3a to your computer and use it in GitHub Desktop.

Select an option

Save chikaibeneme/6ec79ba0d608997b6214c9ef0abb4b3a to your computer and use it in GitHub Desktop.
/**
* Automatically set event categories as post categories for the Events Calendar.
*/
function set_event_categories_as_post_categories( $post_id ) {
// Check if the post is an event post type
if ( 'tribe_events' === get_post_type( $post_id ) ) {
// Get the event categories
$event_categories = wp_get_post_terms( $post_id, 'tribe_events_cat', array( 'fields' => 'names' ) );
// Set event categories as post categories
$post_categories = array();
foreach ( $event_categories as $event_category_name ) {
// Check if the category exists, and if not, create it
$existing_category = get_category_by_slug( sanitize_title( $event_category_name ) );
if ( ! $existing_category ) {
$category_args = array(
'cat_name' => $event_category_name,
'category_nicename' => sanitize_title( $event_category_name ),
);
$existing_category = wp_insert_category( $category_args );
}
// Add the category ID to the post categories
if ( $existing_category ) {
$post_categories[] = $existing_category->term_id;
}
}
// Set the post categories
wp_set_post_categories( $post_id, $post_categories );
}
}
add_action( 'save_post', 'set_event_categories_as_post_categories' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment