Scheduling content unpublishing

It's often useful to allow content contributors to define the period when a content item will be published. If you are on a Professional plan or higher, you can schedule content publishing directly in Kentico Cloud. If you want to define a date when your content will expire and no longer be available in your app, you need to take additional steps.

Table of contents

Implementing scheduled unpublishing is a 3 step process:

Add a Publish until date element to the underlying content type.

Add the unpublish dates to the content items.

Inside your app, check the dates and only display those content items that are supposed to be public.

Let's go through it on a simple example and add this functionality to the Kentico Cloud sample project.

Our sample apps have an Articles section that displays all published articles. We will make a few changes to the sample project and the sample site to show only articles that are in their publishing period.

1. Add unpublish dates to the underlying content type

The content structure of all articles in our project is defined by the Article content type.

From the app menu, choose Content models {@icon-content-models@}.

Click the Article content type to open it.

Drag in a Date & time content element from the right.

Name it Publish until.

You can add helpful guidelines for your content contributors, such as The article should be public until this day.

Adding a Date & time element to a content type

Pro tip

If you need to add the same elements to a lot of content types, use Content snippets.In this case, you can wrap the Publish until date element in a Publishing period content snippet.

Update content models inside the app

To use the newly added unpublish date element inside the sample application, we have to add it to our model class.

2. Define unpublish dates in the content items

Using the filters on the left, select the Article content type to view only articles.

Click an article to edit it.

Fill in the Publish until date element.

Repeat for all articles.

Prepared dates in the Publish until element

3. Only display articles that are supposed to be public now

There are two basic ways to ensure your app only displays articles that should be public:

Adding a filter to your application

Filtering by date with the Delivery API

The main difference is that when you modify your application you can add checks for content with empty dates. Filtering by date with the Delivery API retrieves less content and so should be faster, but will exclude items with null values for Publish until.

Adding a filter to your application

With this technique, you modify your application to check the publish dates of each content item. Currently, the sample app fetches all Article content items.

Add a middle step that filters out all articles that aren't supposed to be public at the time.

It's a good idea to check for null values and implicitly show articles with empty date fields. This allows you to create content that never expires.

That's it. Content contributors can now easily define unpublish dates for their articles.

Filtering by date with the Delivery API

With this technique, you filter items directly in the API call using query parameters. The principle is the same. The only difference is that this approach will not display items that have null values for their unpublish dates. Have a look at the example below:

What's next?

This tutorial showed you how to add unpublish date elements to your content to define the time when it will automatically be unpublished from your app. You can do so inside your application if you want content that doesn't expire, or directly in your API call if you want your content faster and all your content has an expiration date.