Accessing Event Data in Sugar Calendar Events

This tutorial covers how to access event data in Sugar Calendar Events with PHP.


Heads up! This guide contains PHP code and is intended for developers. We offer this code as a courtesy but don’t provide support for code customizations or third-party development.

Before you get started, make sure Sugar Calendar Events is installed and activated on your site. To learn where to add the snippets in this guide, see our tutorial on getting started with Sugar Calendar hooks.

How Sugar Calendar Stores Events

Each event is a WordPress post, but most of its details live in Sugar Calendar’s own database tables:

  • The post: the title, description, featured image, and calendars. Events use the sc_event post type. In Sugar Calendar Events Pro, recurring events use the sc_recurring_event post type.
  • The events table (wp_sc_events): the start and end dates, time zones, all-day setting, recurrence, and venue.
  • The event meta table (wp_sc_eventmeta): extra details such as the location, event URL, and color.

Because Sugar Calendar Events doesn’t store the dates as post meta, use the functions below instead of querying the database directly.

Getting an Event

To load an event, pass its post ID to sugar_calendar_get_event_by_object(). It returns an event object with all the details from the events table:

$event = sugar_calendar_get_event_by_object(
	$post_id,
	'post',
	[ 'object_subtype' => get_post_type( $post_id ) ]
);

if ( $event->exists() ) {
	echo esc_html( $event->title );
}

Note: Always pass the object_subtype argument. Without it, the function only looks for sc_event posts and returns an empty event for recurring events in Sugar Calendar Events Pro.

The event object has these properties:

PropertyDescription
idThe event ID in the events table. This is different from the post ID.
object_idThe post ID.
object_subtypeThe post type, sc_event or sc_recurring_event.
titleThe event title.
contentThe event description.
statusThe post status, such as publish.
startThe start date and time, in Y-m-d H:i:s format.
start_tzThe start time zone. It’s empty when the event uses the site’s time zone.
endThe end date and time, in Y-m-d H:i:s format.
end_tzThe end time zone.
all_dayWhether the event lasts all day.
recurrenceHow often the event repeats: daily, weekly, monthly, yearly, or empty.
recurrence_intervalThe number of days, weeks, months, or years between repeats.
recurrence_countThe number of times the event repeats, if it ends after a set number.
recurrence_endThe date the event stops repeating, if it ends on a date.
venue_idThe venue’s post ID. Venues need Sugar Calendar Events Pro.

Formatting Dates and Times

The start and end properties use the database format. To show a date in any PHP date format, use the start_date() and end_date() methods:

echo esc_html( $event->start_date( 'F j, Y' ) );

if ( ! $event->is_all_day() ) {
	echo esc_html( $event->start_date( 'g:i a' ) . ' to ' . $event->end_date( 'g:i a' ) );
}

This shows “November 14, 2026” and “10:00 am to 12:30 pm.” The is_all_day() method returns true for all-day events, so the example skips the times for those events.

Reading Event Meta

Extra event details live in the event meta table. To read one, pass the event ID (not the post ID) to get_event_meta():

$location = get_event_meta( $event->id, 'location', true );
$url      = get_event_meta( $event->id, 'url', true );

These are the most common event meta keys:

Meta keyDescription
locationThe text in the event’s Location field.
urlThe event URL.
url_target1 when the event URL opens in a new tab.
colorThe event color, as a hex code.
featured1 when the event is featured.

Note: In the Sugar Calendar Events Pro frontend, $event->location returns the venue’s street address when the event has a venue. Use get_event_meta() to always get the Location field.

Getting Calendars and Venues

Calendars are a WordPress taxonomy, so use get_the_terms() with the post ID:

$calendars = get_the_terms( $post_id, sugar_calendar_get_calendar_taxonomy_id() );

if ( ! empty( $calendars ) && ! is_wp_error( $calendars ) ) {
	echo esc_html( implode( ', ', wp_list_pluck( $calendars, 'name' ) ) );
}

Venues are posts too. The venue_id property holds the venue’s post ID, and Sugar Calendar Events stores the venue’s address details as post meta:

if ( ! empty( $event->venue_id ) ) {
	$venue_name = get_the_title( $event->venue_id );
	$venue_city = get_post_meta( $event->venue_id, 'sugarcalendar_venue_city', true );

	echo esc_html( $venue_name . ', ' . $venue_city );
}

The other venue meta keys are sugarcalendar_venue_address_1, sugarcalendar_venue_address_2, sugarcalendar_venue_state, sugarcalendar_venue_postal_code, sugarcalendar_venue_country, sugarcalendar_venue_phone, and sugarcalendar_venue_website.

Querying Events

To get a list of events, use sugar_calendar_get_events(). This example gets the next five published events:

$events = sugar_calendar_get_events(
	[
		'status'      => 'publish',
		'start_query' => [
			[
				'after'     => current_time( 'mysql' ),
				'inclusive' => true,
			],
		],
		'orderby'     => 'start',
		'order'       => 'ASC',
		'number'      => 5,
	]
);

foreach ( $events as $event ) {
	echo esc_html( $event->title . ' (' . $event->start_date( 'M j' ) . ')' );
}

The function returns event objects, so all the properties and methods above work on each result. It accepts other arguments as well, such as object_id, search, and end_query.

Note: The query returns each event once, based on its first date. A recurring event whose first date has passed doesn’t appear in this list, even if it has upcoming dates.

Working With Recurring Events

The recurrence, recurrence_interval, recurrence_count, and recurrence_end properties describe how an event repeats.

To get the individual dates of a recurring event in Sugar Calendar Events Pro, use the get_occurrences() method with the event ID:

$occurrences = \Sugar_Calendar\Pro\Features\AdvancedRecurring\EventSequence::get_occurrences( $event->id, 0 );

foreach ( $occurrences as $occurrence ) {
	echo esc_html( $occurrence->start );
}

Each occurrence has a start and an end date in Y-m-d H:i:s format. The second argument limits the number of results. It defaults to 20, and 0 returns every date.

Note: For events that repeat with no end date, Sugar Calendar Events stores dates for a set period ahead, such as one year for weekly events. The list only includes the dates stored so far.

Using the Legacy Post Meta Keys

Before version 2.0, Sugar Calendar Events stored event dates as post meta. Older code that calls get_post_meta() with these keys still works, because Sugar Calendar Events reads the values from the events table:

Meta keyReturns
sc_event_date and sc_event_date_timeThe start date and time, as a timestamp
sc_event_end_date and sc_event_end_date_timeThe end date and time, as a timestamp
sc_event_day, sc_event_month, sc_event_yearThe start day name, month, and year
sc_event_time_hour, sc_event_time_minute, sc_event_time_am_pmThe start hour, minute, and am or pm
sc_event_end_day, sc_event_end_month, sc_event_end_yearThe end day name, month, and year
sc_event_end_time_hour, sc_event_end_time_minute, sc_event_end_time_am_pmThe end hour, minute, and am or pm
sc_event_timezone and sc_event_end_timezoneThe start and end time zones
sc_event_recurringHow often the event repeats
sc_recur_untilThe date the event stops repeating, as a timestamp

The timestamps hold the event’s local time, so format them with gmdate() to show the correct date and time.

For new code, use the event object instead. These keys don’t return data for recurring events in Sugar Calendar Events Pro, and the old sc_all_recurring key no longer returns anything.

Frequently Asked Questions

These are answers to some of the top questions we receive regarding accessing and querying event data.

How do I count the occurrences of a recurring event?

In Sugar Calendar Events Pro, count the results of get_occurrences():

$count = count( \Sugar_Calendar\Pro\Features\AdvancedRecurring\EventSequence::get_occurrences( $event->id, 0 ) );

Pass 0 as the second argument. Otherwise, the method stops at 20 dates.

Why does my event return no data?

Make sure you’re passing the correct ID. Functions such as sugar_calendar_get_event_by_object() and get_the_terms() take the post ID, while get_event_meta() and get_occurrences() take the event ID from $event->id.

For recurring events in Sugar Calendar Events Pro, also pass the object_subtype argument.

That’s it! You now know how to access event data in Sugar Calendar Events.

Next, would you like to change how Sugar Calendar Events displays your events? Check out our hook reference for the available actions and filters.