Home / posts / Automatically expire posts
  • How to Automatically expire posts

    no thumbnail sorry: Automatically expire posts

    Adding this PHP code to your wordpress theme will allow you to auto expire posts. This does not delete the post however it prevents the post from being displayed within the loop. Create a custom field key called expiration with a date formatted like this mm/dd/yyyy 00:00:00.

    ( click code to copy )

    wordpress snippet : 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…

    ( WordPress codex functions, hooks, in this snippet. )

    the_excerpt, the_title, get_post, have_posts, the_post, get_post_custom, get_post_custom_values, _e, _ex,
  • Selvam

    very cool idea!

    • Cool glad to see you like the wordpress snippet!

  • Diego Loru

    Dear Kevin,
    I’m not a programmer so I got some trouble to use this code but display ALSO today posts. I use it for events, and today events must also show.
    Can you give me a hand please? Thank you, Diego