Make expiring image or html

I’ve built a wordpress site for artwork and would like to set it up so I can create a custom field in the admin that users can select and will display “new” on the front end. Creating this is pretty simple but I would like to “new” to expire after thirty days. Any ideas on setting up the expiration part would be much appreciated.

Related posts

Leave a Reply

1 comment

  1. Assuming you currently have a custom post type archive template (by default, if your custom post type is named “artwork” it would be archive-artwork.php, you could create a check for the date it was published using the_date() function like so:

    // expiration date is a rolling date from 30 days ago
    $expiration_date = date('Ymd', strtotime("-30 days"));
    
    // use the_date( $format, $before, $after, $echo ) to get the current post's
    // date in the loop
    $post_date = the_date('Ymd', '', '', false);
    
    // compare the dates and output different markup or classes to your HTML
    if ($post_date < $expiration_date) {
        echo '<div class="expired-post">';
    }
    else {
        echo '<div class="new-post">';
    }