Posts created by CRED forms can be set to automatically expire after a specified period. This is useful when you have content that can be created by users and you only want the post to be visible for a specified period.

An example would be a site that accepts adverts from users. The user might pay for placing the advert on the site, and the advert might only be shown for 30 days.

Enabling Automatic Expiration of Posts

Automatic expiration of posts can be enabled from the Forms tab of the Toolset Settings page. You can choose to have posts checked to see if they have reached their expiration date once hourly, twice daily, or once daily.

CRED settings for post expiration
CRED settings for post expiration

Using Automatic Expiration on CRED forms

The CRED form editor has an option in the Settings section for setting an Automatic Expiration Date per form. From here, you can enable automatic expiration for a form and set the expiration time. The expiration time can be set in minutes, hours, days or weeks.

You can also set what happens after expiration. The new post status can be set to the following:

  • Keep original status
  • Draft
  • Pending review
  • Private
  • Published
  • Trash

CRED post expiration options in a form
CRED post expiration options in a form

Sending email notifications when posts expire

When automatic expiration is enabled for a form a new option is available in the notifications for the CRED form. You can choose to send a notification message in an amount of minutes, hours, days or weeks before the post will expire. We could use this to notify the user that created the advert that it is going to expire in 5 days.

CRED settings for email notifications when posts expire
CRED settings for email notifications when posts expire

Post Expiration settings in the WordPress post editor

When CRED creates posts that are set to expire, the expiration time is shown in the post editor.

You can manually change the expiration date and status for each post.

Post Expiration settings in the WordPress post editor
Post Expiration settings in the WordPress post editor

Displaying information about the expiration date on the front-end

You can display the expiration time for posts by using [cred-post-expiration] shortcode, with the following optional attributes:

  • id => post ID, Defaults to the current post being displayed
  • format => Format string for the date. Defaults to WordPress date format settings (documentation on date formatting)

An example usage of the shortcode is:

[cred-post-expiration id="15" format="m/d/Y"]

This would display the expiration date of the post with an ID of 15 using the month/day/YEAR format.

Customizing Automatic Post Expiration using filters

Adding additional post expiration schedules

You can choose one of the following schedules in the settings page:

  • once hourly
  • twice daily
  • once daily.

You can also define your custom schedule using a filter:

Using a filter to define a custom schedule
add_filter ('cred_post_expiration_cron_schedules', 'my_function', 10,1);

function my_function ($schedules) {
	$schedules['fifteenmin'] = array( 'interval' => 15 * MINUTE_IN_SECONDS, 'display' => __( 'Every 15 minutes' ) );
	return $schedules;
}

This adds a schedule that runs every 15 minutes.

What happens when posts expire

When posts expire, their status is set to what you have selected via the GUI. You can also set post meta values by using the ‘cred_post_expiration_custom_actions’ filter.

add_filter('cred_post_expiration_custom_actions', 'my_custom_actions', 10, 3);

Parameters

$custom_actions – an array of actions where custom actions can be added. Each action is an associative array with two elements (meta_key and meta_value)
$post_id – ID of the currently created or edited post.
$form_data – associative array of data about the current form (form_id, post_type and form_type).

The follwoing example sets the advert_active field to false when the post expires.

Updating a custom field when a post expires
function my_custom_actions($custom_actions, $post_id, $form_data) {
	$custom_actions[] = array( 'meta_key' => 'advert_active', 'meta_value' =>'false' );
	return $custom_actions;
}

Seeing how CRED’s post expiration works on a live site

The post expiration feature includes many options and allows you to implement different workflows. It is useful when you want user-submitted content to automatically update after a certain time.

You can see it in action in our classifieds reference site. Visitors (and paying clients) can submit ads. These ads will be displayed for a month (which you can change in CRED’s settings), and then they will change to drafts and go off-line.