Home » Labs » How to auto expire posts in WordPress without plugin  

How to auto expire posts in WordPress without plugin


Filed under Labs

You can set auto expire option for posts in WordPress by adding following code to your theme. The code will not delete you posts but prevents it from displaying within a loop. For doing this you need to create a custom field called expiration with a date formatted like mm/dd/yyyy 00:00:00.

 
  <?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;
    ?>

Posts you may like: