Results 1 to 2 of 2

Set An Expiration Time For Posts [tutorial]

This is a discussion on Set An Expiration Time For Posts [tutorial] within the Blogging forums, part of the Internet Marketing category; ...

  1. #1
    Member
    Join Date
    Mar 2012
    Posts
    72

    Default Set An Expiration Time For Posts [tutorial]

    Sometimes (for example, if you’re running a contest), you want to be able to publish a post and then automatically stop displaying it after a certain date. This may seem quite hard to do but in fact is not, using the power of custom fields.

    Edit your theme and replace your current WordPress loop with this “hacked” loop:

    php Code:

    <?php
    if (have_posts()) :
    while (have_posts()) : the_post(); ?>
    $expirationtime = get_post_custom_values('expiration');
    if (is_array($expirationtime)) {
    $expirestring = implode($expirationtime);
    }
    $secondsbetween = strtotime($expirestring)-time();
    if ( $secondsbetween > 0 ) {
    // For example...
    the_title();
    the_excerpt();
    }
    endwhile;
    endif;
    ?>

    To create a post set to expire at a certain date and time, just create a
    custom field. Specify expiration as a key and your date and time as a value
    (with the format mm/dd/yyyy 00:00:00). The post will not show up after the
    time on that stamp.
    Code explanation. This code is simply a custom WordPress loop that
    automatically looks to see if a custom field called expiration is present. If
    one is, its value is compared to the current date and time.
    If the current date and time is equal to or earlier than the value of the
    custom expiration field, then the post is not displayed.
    Note that this code does not remove or unpublish your post, but just
    prevents it from being displayed in the loop

  2. #2
    Member
    Join Date
    Nov 2011
    Posts
    63

    Default Re: Set An Expiration Time For Posts [tutorial]

    Ooh, this is quite appropriate for mini-blog announcements. And for blog claim posts. Thank you for this useful tip.

Tags for this Thread