Auto expire entries - Hiranyaloka/Documentation GitHub Wiki

This recipe provides a way to auto-expire entries on a given date.

Table of Contents

The Problem

How to have an entry automatically not display (moved to archives) on a given day, or a specified number of days after it was first published.

Typical uses

To display notices with a limited 'shelf-life', those that are relevant for a few days. For instance, a notice about an upcoming event that ceases to be relevant once the date of that event has passed.

The logic

  1. Note the expiry date of the to-be-expired post, as a variable in a custom-field
  2. Compare the post expiry date to the current-date;
  3. If the current date is the post-expiry date of the post, don't show the post;
  4. Alternatively, expire the post X days after its published date.

Alternatives

There are two ways to do this.

1. Use MT's native <mt:if> logic function. Convert the dates into numerical strings and compare them. 2. Use a plugin.

Methods

Method 1: Using MT's native <mt:if> logic function

  1. Set up a custom field called noticeexpirydate and set its format to date only.
  2. Use the following code
</mt:if> </mt:entries> </pre>

Explanation

  1. The entries are those limited to the category "Notices".
  2. You can limit them further to "sticky"notices using private tags.
  3. Get today's date and convert it into a YYYYMMDD format
  4. Convert the custom field expiry date into the YYYYMMDD format
  5. If today's date is less than the current date, then and only then, show the post.

Method 2: Use a plugin

  1. Get and install the DateTags plugin from staggernation;
  2. Set up a custom field called noticeexpirydate and set its format to date only.
  3. Use a code along these lines
		<mt:ifnonempty tag="NoticeExpiryDate">
		
		<MTIfDateBeforeOrEqual date="[MTDate format='%m/%d/%y']" target="[MTNoticeExpiryDate format='%m/%d/%y']">
			
		</mtifdatebeforeorequal>
		<mt:else>
		
		<MTIfDateWithin date="[MTDate format='%m/%d/%y']" start="[MTEntryDate format='%m/%d/%y']" end="[MTEntryDate format='%m/%d/%y']" end_adjust="10">
			
		</mtifdatewithin>
		</mt:ifnonempty>
        </mtpages>

Explanation

This showcases the many powerful features of DateTags. If an expiry date has not been explicitly set, then it takes a range of dates (entry date, and entry date adjusted forward by N, in this case 10, days) and checks to see if today falls within that range.

This works beautifully even on MT4, though designed for earlier versions. It does not work with dynamic publishing. To get it to work with dynamic publishing, you might push out the code as a separate static file and then include it with SSI or a PHP include function into a dynamic page.

Archiving

Coupled with a Cron job for rebuilding nightly, this ensures that notices expire when scheduled.

Gautam Patel 07:26, 20 September 2009 (UTC)

Category: Template Tag Recipes

⚠️ **GitHub.com Fallback** ⚠️