Live Events export for Google Calendar

Learn how to generate ICS calendar files using Advanced Views, enabling users to download events or subscribe to a live calendar feed that stays up to date.

1. Introduction

Advanced Views excels at displaying content. In most cases, this means presenting queried content in the browser as HTML.

However, Advanced Views is designed with format-agnostic Layouts and Post Selections. This means you can output your content in virtually any text format, including:

  • XML

  • CSV

  • JSON

This flexibility allows you to provide native data formats for external tools, including calendar applications.

2. Task definition

Let’s review a practical example.

Suppose you have an Events CPT (Custom Post Type) with two ACF date fields:

  • Start_Date

  • End_Date

Both fields are assigned to the Events CPT.

Our goal is to create a link that allows visitors to download and import events into their calendar.

Since this content is date-related, the exported file should include only upcoming events, respecting the current date.

Creating such a file manually would be inefficient. It would require:

  • constant copy-paste work

  • manual updates whenever events change

Instead, we will generate the file dynamically using Advanced Views.

3. Implementation overview

We will define two components:

"Event" Layout

Responsible for rendering a single event entry inside the exported file.

"Events" Post Selection

Responsible for:

  • querying the Events CPT

  • assembling all event entries into a file

Unlike typical Post Selections, we won't embed the Post Selection shortcode into a page. Instead, we will use the Advanced Views Ajax integrationarrow-up-right.

This integration gives us a unique URL tied to the Post Selection, which can be placed anywhere on your site. For example, you can attach this URL to a download link.

circle-info

Pro tip: Visitors can add this URL in their preferred Calendar app using the Import (one time import, no event updates) or From URL (live event updates).

4. Implementation

Most calendar applications accept the standard ics format, where Google Calendar supports two import formatsarrow-up-right:

  • .ics

  • .csv

Because the automatically generated Layout and Post Selection templates are HTML-based, we need to customize them to output the correct formats. Below, are examples for both formats.

4.1) Layout

  1. Create a new "Event" Layout

  2. In the Fields tab, assign the fields:

    • ACF field Start Date (date_picker)

    • ACF field End Date (date_picker)

    • Post Title Field.

    • Post Content.

  3. Click Publish to Save the layout.

The default template will now be generated.

Switch to the Template tab.

You will see something similar to this:

Next, we need to modify the default template to match our export format.

A. ICS format

The ICS type expects the "FIELD: value" format.

Example Twig template:

Don't forget to replace YOUR_NAME with your unique name and ensure your field names match.

B. CSV format

For CSV, values are separated by commas. Example Twig template:

4.2) Post Selection template

Next create the Post Selection to sort and filter your event posts.

  1. Create a new "Events" Post Selection

  2. Set the 'Event' Layout as the Item Layout

  3. For Post Type select 'event' to filter by.

  4. Click Publish to Save your post selection.

Switch to the Template tab to see the generated default template.

Example Twig template:

Next, we need to replace this default template to match our export format.

A. ICS format

Copy the below template into the Custom Template field. No extra editing required.

Twig example:

B. CSV format

Copy the below template into the Custom Template field. No extra editing required.

Twig example:

See the Google Calendar documentation for the full list of supported CSV fieldsarrow-up-right.

4.3) Post Selection Ajax

The Post Selection is now ready.

However, we still need a URL endpoint that returns the generated file.

To achieve this, we will add an Ajax handlerarrow-up-right using the Custom Data field in the Advanced tab.

Use the following code as a base:

Make sure to:

  • Copy the Post Selection shortcode from the current Post Selection

  • Paste it into the do_shortcode() call

You could also change the exported filename by editing $filename = "events.ics".

Save your Post Selection.

5. Verifying the result

Now we're ready to test the calendar export. Open a new browser tab and visit:

Replace:

  • YOUR_DOMAIN

  • YOUR_POST_SELECTION_ID

with your actual values.

Visiting this URL will trigger the Ajax handler and download the generated file, which contains real-time data from the WordPress database.

Below is what my events.ics file contains.

circle-info

Note: admin-ajax.php is the standard WordPress Ajax URL base. Although the URL contains /wp-admin/, it works for both guests and authenticated users. If necessary, you can restrict access by adding a permission check at the top of the Ajax handler.

lightbulb-exclamation-on

Last updated