Title: Expire Posts
Last modified: August 18, 2016

---

# Expire Posts

 *  [adirotea](https://wordpress.org/support/users/adirotea/)
 * (@adirotea)
 * [20 years, 6 months ago](https://wordpress.org/support/topic/expire-posts/)
 * I have made a new plugin that implements “expireable” posts and pages.
 * Looking for feedback and beta testing.
 * Hope that this plugin to contribute to some core changes in WordPress (previous_post_link
   and next_post_link should, in my opinion, apply the custom filters set to post
   fetching query).
 * Regards,
    Adrian
 * `
    <?php /* Plugin Name: Rotea Expire Posts Plugin URI: http://wordpress.org/#
   Description: This plugin filters the posts, hiding the expired ones from the 
   site only and *NOT* from admin section too! The post expiration is triggered 
   by the custom field <code>expiration</code> attached to the post. If <code>expiration
   </code> is *NOT* set then that post will never expire. Due to some not implemented
   behaviuors in WordPress you *MUST* use <code>rotea_previous_post_link</code> 
   and <code>rotea_next_post_link</code> instead of <code>previous_post_link</code
   > and <code>next_post_link</code> in your templates. Author: Adrian Rotea <adrian.
   rotea@gmail.com> Version: 1.0 Author URI: mailto:adrian.rotea@gmail.com */
 * // some constants
    define('EXPIRATION_META', 'expiration'); // post meta key 
   used in the wp database
 * // expirtion form field
    function rotea_expire_posts_form_field() { global $month,
   $postdata, $content;
 *  $time_adj = time() + (get_settings('gmt_offset') * 3600);
    $post_date = get_post_meta(
   $postdata->ID, EXPIRATION_META, true);
 *  if (!empty($post_date)) {
    $checked = ' checked="checked" '; $edit = true; }
   else { $checked = ' '; $edit = false; }
 *  echo "
    <script type=\"text/javascript\"> function toggle_expiration_edit(form,
   enable) { if (enable) { form.elements['expiration[mm]'].disabled = false; form.
   elements['expiration[jj]'].disabled = false; form.elements['expiration[aa]'].
   disabled = false; form.elements['expiration[hh]'].disabled = false; form.elements['
   expiration[mn]'].disabled = false; form.elements['expiration[ss]'].disabled =
   false; } else { form.elements['expiration[mm]'].disabled = true; form.elements['
   expiration[jj]'].disabled = true; form.elements['expiration[aa]'].disabled = 
   true; form.elements['expiration[hh]'].disabled = true; form.elements['expiration[
   mn]'].disabled = true; form.elements['expiration[ss]'].disabled = true;  } } 
   </script>  "; echo '<table border="0" cellspacing="3" cellpadding="3" width="
   100%"><tr><th width="1%">Expiration:</th><td width="99%">'; echo '<fieldset><
   legend><input type="checkbox" class="checkbox" name="expiration[edit_date]" value
   ="1" id="expiration[timestamp]" ' . $checked . ' onClick="toggle_expiration_edit(
   this.form, this.checked);" /> <label for="expiration[timestamp]">' . __('Edit
   timestamp') . '</label></legend>';
 *  $jj = ($edit) ? mysql2date('d', $post_date) : gmdate('d', $time_adj);
    $mm =(
   $edit) ? mysql2date('m', $post_date) : gmdate('m', $time_adj); $aa = ($edit) ?
   mysql2date('Y', $post_date) : gmdate('Y', $time_adj); $hh = ($edit) ? mysql2date('
   H', $post_date) : gmdate('H', $time_adj); $mn = ($edit) ? mysql2date('i', $post_date):
   gmdate('i', $time_adj); $ss = ($edit) ? mysql2date('s', $post_date) : gmdate('
   s', $time_adj);
 *  echo "<select name=\"expiration[mm]\" " . ($edit ? '' : 'disabled') . " >n";
   
   for ($i=1; $i < 13; $i=$i+1) { echo "ttt<option value=\"$i\""; if ($i == $mm)
   echo " selected='selected'"; if ($i < 10) { $ii = "0".$i; } else { $ii = "$i";}
   echo ">".$month["$ii"]."</option>n"; }
 * ?>
 * </select>
    <input type="text" name="expiration[jj]" value="<?php echo $jj; ?>"
   size="2" maxlength="2" <?php echo ($edit ? '' : 'disabled') ?> /> <input type
   ="text" name="expiration[aa]" value="<?php echo $aa ?>" size="4" maxlength="5"
   <?php echo ($edit ? '' : 'disabled') ?> /> @  <input type="text" name="expiration[
   hh]" value="<?php echo $hh ?>" size="2" maxlength="2" <?php echo ($edit ? '' :'
   disabled') ?> /> :  <input type="text" name="expiration[mn]" value="<?php echo
   $mn ?>" size="2" maxlength="2" <?php echo ($edit ? '' : 'disabled') ?> />  <input
   type="hidden" name="expiration[ss]" value="<?php echo 59/*$ss*/ ?>" size="2" 
   maxlength="2" <?php echo ($edit ? '' : 'disabled') ?> />
 * <?php
    // We might need to readjust to display proper existing timestamp if (
   $for_post && ('draft' == $postdata->post_status) ) { $jj = mysql2date('d', $post_date);
   $mm = mysql2date('m', $post_date); $aa = mysql2date('Y', $post_date); $hh = mysql2date('
   H', $post_date); $mn = mysql2date('i', $post_date); $ss = mysql2date('s', $post_date);}
 *  echo ($edit ? __('Existing timestamp') . ": {$month[$mm]} $jj, $aa @ $hh:$mn":'');
   ?
   >
 *  </fieldset>
    </td> </tr> </table> <?php }
 * // form function
    function rotea_expire_posts_form()  { rotea_expire_posts_form_field();}
 * // save function
    function rotea_expire_posts_save($id)  { // remove old value
   delete_post_meta($id, EXPIRATION_META);
 *  // clean up expiration date 
    if (!empty($_POST['expiration']['edit_date'])){
   $aa = $_POST['expiration']['aa']; $mm = $_POST['expiration']['mm']; $jj = $_POST['
   expiration']['jj']; $hh = $_POST['expiration']['hh']; $mn = $_POST['expiration']['
   mn']; $ss = $_POST['expiration']['ss'];
 *  $jj = ($jj > 31) ? 31 : $jj;
    $hh = ($hh > 23) ? $hh - 24 : $hh; $mn = ($mn 
   > 59) ? $mn - 60 : $mn; $ss = ($ss > 59) ? $ss - 60 : $ss;
 *  $expiration = "$aa-$mm-$jj $hh:$mn:$ss"; // local
    //$expiration = get_gmt_from_date("
   $aa-$mm-$jj $hh:$mn:$ss"); //gmt } else { $expiration = ''; }
 *  // save
    if (!empty($expiration)) { add_post_meta($id, EXPIRATION_META, $expiration);}}
 * // where function
    function rotea_expire_posts_where($where)  {  if (!is_admin()){
   global $wpdb;
 *  //$where .= " AND IF($wpdb->postmeta.meta_id IS NULL, 1, $wpdb->postmeta.meta_value
   >= '" . gmdate('Y-m-d H:i:59') . "') "; 
    $where .= " AND IF($wpdb->postmeta.
   meta_id IS NULL, 1, $wpdb->postmeta.meta_value >= NOW()) ";  }
 *  return $where; 
    }
 * // join function
    function rotea_expire_posts_join($join)  { if (!is_admin()){
   global $wpdb;
 *  $join .= " LEFT JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id
   AND $wpdb->postmeta.meta_key = 'expiration') ";
    }
 *  return $join;
    }
 * // Navigation links
    function rotea_get_previous_post($in_same_cat = false, $
   excluded_categories = '') { global $post, $wpdb;
 *  if(! is_single()) {
    return null; }
 *  $current_post_date = $post->post_date;
 *  $join = '';
    if ($in_same_cat) { $join = " INNER JOIN $wpdb->post2cat ON $wpdb-
   >posts.ID= $wpdb->post2cat.post_id "; $cat_array = get_the_category($post->ID);
   $join .= ' AND (category_id = ' . intval($cat_array[0]->cat_ID); for ($i = 1;
   $i < (count($cat_array)); $i++) { $join .= ' OR category_id = ' . intval($cat_array[
   $i]->cat_ID); } $join .= ')';  }
 *  // Apply filters on join prior to paging so that any
    // manipulations to them
   are reflected in the paging by day queries. $join = apply_filters('posts_join',
   $join);
 *  $sql_exclude_cats = '';
    if (!empty($excluded_categories)) { $blah = explode('
   and', $excluded_categories); foreach($blah as $category) { $category = intval(
   $category); $sql_exclude_cats .= " AND post_category != $category"; } }
 *  // Apply filters on where prior to paging so that any
    // manipulations to them
   are reflected in the paging by day queries.  $where = apply_filters('posts_where',
   $where);
 *  return @$wpdb->get_row("SELECT ID, post_title FROM $wpdb->posts $join WHERE 
   post_date < '$current_post_date' AND post_status = 'publish' $where $sqlcat $
   sql_exclude_cats ORDER BY post_date DESC LIMIT 1");
    }
 * function rotea_get_next_post($in_same_cat = false, $excluded_categories = ''){
   
   global $post, $wpdb;
 *  if(! is_single()) {
    return null; }
 *  $current_post_date = $post->post_date;
 *  $join = '';
    if ($in_same_cat) { $join = " INNER JOIN $wpdb->post2cat ON $wpdb-
   >posts.ID= $wpdb->post2cat.post_id "; $cat_array = get_the_category($post->ID);
   $join .= ' AND (category_id = ' . intval($cat_array[0]->cat_ID); for ($i = 1;
   $i < (count($cat_array)); $i++) { $join .= ' OR category_id = ' . intval($cat_array[
   $i]->cat_ID); } $join .= ')';  }
 *  // Apply filters on join prior to paging so that any
    // manipulations to them
   are reflected in the paging by day queries. $join = apply_filters('posts_join',
   $join);
 *  $sql_exclude_cats = '';
    if (!empty($excluded_categories)) { $blah = explode('
   and', $excluded_categories); foreach($blah as $category) { $category = intval(
   $category); $sql_exclude_cats .= " AND post_category != $category"; } }
 *  // Apply filters on where prior to paging so that any
    // manipulations to them
   are reflected in the paging by day queries.  $where = apply_filters('posts_where',
   $where);
 *  $now = current_time('mysql');
 *  return @$wpdb->get_row("SELECT ID,post_title FROM $wpdb->posts $join WHERE post_date
   > '$current_post_date' AND post_date < '$now' AND post_status = 'publish' $where
   $sqlcat $sql_exclude_cats AND ID != $post->ID ORDER BY post_date ASC LIMIT 1");
   }
 * function rotea_previous_post_link($format='&laquo; %link', $link='%title', $in_same_cat
   = false, $excluded_categories = '') {
    $post = rotea_get_previous_post($in_same_cat,
   $excluded_categories);
 *  if(! $post) {
    return; }
 *  $title = apply_filters('the_title', $post->post_title, $post);
 *  $string = '<a href="'.get_permalink($post->ID).'">';
 *  $link = str_replace('%title', $title, $link);
 *  $link = $string . $link . '</a>';
 *  $format = str_replace('%link', $link, $format);
 *  echo $format; 
    }
 * function rotea_next_post_link($format='%link &raquo;', $link='%title', $in_same_cat
   = false, $excluded_categories = '') {
    $post = rotea_get_next_post($in_same_cat,
   $excluded_categories);
 *  if(! $post) {
    return; }
 *  $title = apply_filters('the_title', $post->post_title, $post);
 *  $string = '<a href="'.get_permalink($post->ID).'">';
 *  $link = str_replace('%title', $title, $link);
 *  $link = $string . $link . '</a>';
 *  $format = str_replace('%link', $link, $format);
 *  echo $format; 
    }
 * // link function
    /* function rotea_expire_posts_link($title, $post)  { $expiration
   = get_post_meta($post->ID, EXPIRATION_META, true);
 *  if (!empty($expiration) && intval($expiration = strtotime($expiration)) > 0 &&
   current_time('timestamp') >= $expiration) { 
    return NULL; }
 *  return $title;
    }
 * // prev/next link
    //add_filter('post_link', 'rotea_expire_posts_link', 10, 2);//
   add_filter('the_title', 'rotea_expire_posts_link', 10, 2); */
 * // form 
    add_action('simple_edit_form', 'rotea_expire_posts_form'); add_action('
   edit_form_advanced', 'rotea_expire_posts_form'); add_filter('edit_page_form','
   rotea_expire_posts_form');
 * // save
    add_action('edit_post', 'rotea_expire_posts_save'); add_action('publish_post','
   rotea_expire_posts_save'); add_action('save_post', 'rotea_expire_posts_save');
 * // query
    add_filter('posts_where', 'rotea_expire_posts_where'); add_filter('
   posts_join', 'rotea_expire_posts_join'); //add_filter('posts_where_paged', 'rotea_expire_posts_where');//
   add_filter('posts_join_paged', 'rotea_expire_posts_join');
 * ?>

Viewing 15 replies - 1 through 15 (of 16 total)

1 [2](https://wordpress.org/support/topic/expire-posts/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/expire-posts/page/2/?output_format=md)

 *  [Mark (podz)](https://wordpress.org/support/users/podz/)
 * (@podz)
 * [20 years, 6 months ago](https://wordpress.org/support/topic/expire-posts/#post-289129)
 * Please post this on your site as either a zip or a .phps
 * It is virtually unusable here.
 *  Thread Starter [adirotea](https://wordpress.org/support/users/adirotea/)
 * (@adirotea)
 * [20 years, 6 months ago](https://wordpress.org/support/topic/expire-posts/#post-289137)
 * Done:
 * [http://www.sitpic.com/rotea-expire-posts.phps](http://www.sitpic.com/rotea-expire-posts.phps)
 *  Thread Starter [adirotea](https://wordpress.org/support/users/adirotea/)
 * (@adirotea)
 * [20 years, 6 months ago](https://wordpress.org/support/topic/expire-posts/#post-289362)
 * Updated.
 * [http://www.sitpic.com/rotea-expire-posts.phps](http://www.sitpic.com/rotea-expire-posts.phps)
 * Added filter for list_cats to calculate the number of filtered (*NOT* expired)
   posts only, because WordPress doesn’t apply the filters added to get_posts to
   the categories count fetching.
 * Adrian
 *  Thread Starter [adirotea](https://wordpress.org/support/users/adirotea/)
 * (@adirotea)
 * [20 years, 6 months ago](https://wordpress.org/support/topic/expire-posts/#post-289363)
 * Updated.
 * [http://www.sitpic.com/rotea-expire-posts.phps](http://www.sitpic.com/rotea-expire-posts.phps)
 * Added filter for list_cats to calculate the number of filtered (*NOT* expired)
   posts only, because WordPress doesn’t apply the filters added to get_posts to
   the categories count fetching.
 * Adrian
 *  [jmesam](https://wordpress.org/support/users/jmesam/)
 * (@jmesam)
 * [20 years, 6 months ago](https://wordpress.org/support/topic/expire-posts/#post-289670)
 * Thank you very much for this plugin it’s perfect! But there’s a problem with 
   gmt offset variable, it shows my server time (GMT+1), not my time (GMT). And 
   wp time options are configured ok.
 *  [jmesam](https://wordpress.org/support/users/jmesam/)
 * (@jmesam)
 * [20 years, 6 months ago](https://wordpress.org/support/topic/expire-posts/#post-289671)
 * Oops! Your plugin was ok, it was my wp configuration 8-|
 * Thanks again for this plugin.
 *  [richschmidt](https://wordpress.org/support/users/richschmidt/)
 * (@richschmidt)
 * [20 years, 6 months ago](https://wordpress.org/support/topic/expire-posts/#post-289672)
 * That link seems to not be working… It’s asking for username and password…
 *  [jmesam](https://wordpress.org/support/users/jmesam/)
 * (@jmesam)
 * [20 years, 6 months ago](https://wordpress.org/support/topic/expire-posts/#post-289674)
 * If you try to download it look for it in google cache.
 *  [hgw](https://wordpress.org/support/users/hgw/)
 * (@hgw)
 * [20 years, 5 months ago](https://wordpress.org/support/topic/expire-posts/#post-289707)
 * This is great and I was looking for that since WordPress 1.0.2. Unfortunately
   it seems that your page isn’t in the Google cache any more.
 * I copied your code above and made a expires.php file in my plugins directory,
   seems that it’s working. But maybe I’m missing a great or important update to
   your plugin?
 *  [polyfade](https://wordpress.org/support/users/polyfade/)
 * (@polyfade)
 * [20 years, 5 months ago](https://wordpress.org/support/topic/expire-posts/#post-289719)
 * This plugin doesn’t seem to be working with WordPress 2.0. The post still shows
   up. I have the postmeta viewable to make sure the ‘expiration’ has been checked.
   
   Is there an update or am I missing something?
 *  [hgw](https://wordpress.org/support/users/hgw/)
 * (@hgw)
 * [20 years, 4 months ago](https://wordpress.org/support/topic/expire-posts/#post-289726)
 * Upps, it doesn’t seem to work anymore, it worked until 31-12-2005, in WP 2, with
   some posts. Now no more??
 *  [ttech5593](https://wordpress.org/support/users/ttech5593/)
 * (@ttech5593)
 * [20 years, 4 months ago](https://wordpress.org/support/topic/expire-posts/#post-289727)
 * Hmm all I get with wordpress 2.01 is an error. Any news on a new version ? Thanks…
 *  [WPChina](https://wordpress.org/support/users/wordpresschina/)
 * (@wordpresschina)
 * [19 years, 12 months ago](https://wordpress.org/support/topic/expire-posts/#post-289736)
 * I am using WP 2.0.3 and this plugin seems not to work.
 * Does anyone know of a plugin that allow for me to “expire” posts on a post-by-
   post basis. For example, most of my posts I do NOT want to expire, but some I
   want to only be available to users for a few months.
 * Tks!
 *  [mggl](https://wordpress.org/support/users/mggl/)
 * (@mggl)
 * [19 years, 12 months ago](https://wordpress.org/support/topic/expire-posts/#post-289737)
 * I found this one earlier this year but **have not tested it** on WP2.0.3:
    [http://wunder-ful.com/wordpress-plugins](http://wunder-ful.com/wordpress-plugins)(
   I dont’ think it’s the same one…)
 *  [manstraw](https://wordpress.org/support/users/manstraw/)
 * (@manstraw)
 * [19 years, 11 months ago](https://wordpress.org/support/topic/expire-posts/#post-289739)
 * I just tested it, and it seems to work 2.0.3. I can’t seem to get the orderby
   list to work. I’m getting a 404. Possibly a permalinks issue. I’m not going to
   check that out right now.

Viewing 15 replies - 1 through 15 (of 16 total)

1 [2](https://wordpress.org/support/topic/expire-posts/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/expire-posts/page/2/?output_format=md)

The topic ‘Expire Posts’ is closed to new replies.

## Tags

 * [expiration](https://wordpress.org/support/topic-tag/expiration/)

 * 16 replies
 * 10 participants
 * Last reply from: [WPChina](https://wordpress.org/support/users/wordpresschina/)
 * Last activity: [19 years, 7 months ago](https://wordpress.org/support/topic/expire-posts/page/2/#post-289750)
 * Status: not a support question

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
