Set An Expiration Time For Posts [tutorial]

Discussion in 'WordPress' started by lordoracle, Apr 17, 2012.

  1. #1
    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
    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;
    ?>
    PHP:
    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



    Easy.
     
    lordoracle, Apr 17, 2012 IP
  2. DayaJosh

    DayaJosh Member

    Messages:
    198
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    28
    #2
    Thanks for the steps :) , It may useful for my team, I will keep this in handy.
     
    DayaJosh, Apr 17, 2012 IP
  3. lordoracle

    lordoracle Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    There will be more. ;)
     
    lordoracle, Apr 17, 2012 IP
  4. StreetFighter

    StreetFighter Peon

    Messages:
    27
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Didn't think of doing that. Might be useful. Thanks for sharing.
     
    StreetFighter, May 8, 2012 IP
  5. dilpearl

    dilpearl Well-Known Member

    Messages:
    117
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    108
    #5
    Thanks for this tips
     
    dilpearl, May 8, 2012 IP