2

Can I somehow set the expiration date of an entry based on a custom-field. I ask this because I don't want the site-owner to have to manually set the expiration date (for multiple reasons)but they are setting a 'start' date in a custom field. I am currently outputting entries and limiting them by doing this:

{% for event in entries %}
                    {% set count = 0 %}
                    {% if count < 5 and event.dateTime|date_modify('tomorrow 5am')|date('U') > now|date('U') %}

It would be really fantastic though if i was able to use that same date_modify to expire the entry and remove the entries from the loop all-together due to being expired.

3

You could write a fairly simple plugin to automatically set the expiration based off of another field value or calculation.

In your plugin's main file:

public function init() {

    parent::init();

    craft()->on('entries.saveEntry', function(Event $event) {

        $entry = $event->params['entry'];

        if ($entry->section == 'Events') {

            if (empty($entry->expiryDate)) {
                $date = new DateTime($entry->dateTime);
                $modifiedDate = date_modify($date, 'tomorrow 5am');
                $entry->expiryDate = $modifiedDate;
                craft()->entries->saveEntry($entry);
            }

        }

    });

}

Updated Code fixed syntax errors and added a check to see if expiration date is already set, otherwise created an endless loop (and may overwrite a manual setting).

1
  • You're on fire today, Douglas... I can't keep up.
    – Brad Bell
    Apr 26 '15 at 0:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.