How to give your WordPress posts an expiration date

FerdieQO

Well-known member
Joined
Jul 15, 2016
Messages
213
Points
28
This WordPress tip becomes very useful when you are running a short-term promotion that does not want it to stay forever. Instead of manually removing posts, you can set the calendar to stop itself from appearing. You can apply this to an article on your site. You post it on your blog, but you do not want this program to stay on your blog after the program ends. So you can remove it automatically with this code.

All you need to do is replace your WordPress Loop with this code:

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;
?>
Then you can use custom fields when writing a post to set expiration dates. Make sure the "Expiration" key and use the following date format: mm / dd / yyyy 00:00:00

This WordPress technique does not remove or unpublish posted posts; it just does not let the post show up on feed anymore.
 

Dracindo

Member
Registered
Joined
Nov 22, 2018
Messages
48
Points
8
It's been a while since I've done some programming, but this part confuses me:
Code:
while (have_posts()) : the_post(); ?>
From what I understand, <? is an opening and ?> a closure, but it's a while loop, so there's code coming after this line.
Doesn't the ?> close the bracket too early?

I try to pick up programming again, so I'd love to have code questions explained to me.
 
Recommended Threads
Replies
14
Views
5,694
Replies
2
Views
1,850

Latest Hosting OffersNew Reviews

Sponsors

Tag Cloud

You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an alternative browser.

Top