Setting expiration date for posts: need help editing wordpress loop

Hi there!
I’m needing some help editing my Wordpress loop to not show posts after they have expired (using a custom field). I’ve found this piece of code to use:

    <?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 exemple...   the_title();
            the_excerpt();
            }
        endwhile;
    endif;
    ?>  

and I need to integrate it into my custom loop…

<?php query_posts('cat=4,1'); ?>

<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<!-- thumbnail code below -->
<?php if ( has_post_thumbnail() ) { ?>
<a href="<?php the_permalink() ?>"><?php the_post_thumbnail(); ?></a>
<?php } else { ?>
<?php } ?>
<!--end thumbnail code-->
<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
<p class="smallprint">Posted on <?php the_time('F jS, Y') ?> <!-- by <?php the_author() ?> --></p>
<!-- shows excerpts below -->
<?php
global $more;
$more = 0;
?>
<!-- end shows excerpts code -->			
<?php the_content('Read the rest of this entry &raquo;'); ?>
<p class="post_footer"><?php the_tags('Tags: ', ', ', '<br />'); ?> Posted in <?php the_category(', ') ?> | <?php edit_post_link('Edit'); ?>  </p>
</div>
<?php endwhile; endif; ?>	

Can someone please help me out?



<?php query_posts('cat=4,1'); ?> 

<?php if (have_posts()) : ?> 
<?php while (have_posts()) : the_post(); ?> 
<?php $expirationtime = get_post_custom_values('expiration'); 
    if (is_array($expirationtime)) {
        $expirestring = implode($expirationtime);         
    }
         $secondsbetween = strtotime($expirestring)-time();         
            if ( $secondsbetween > 0 ) { ?><div <?php post_class() ?> id="post-<?php the_ID(); ?>"> 
<!-- thumbnail code below --> 
<?php if ( has_post_thumbnail() ) { ?> 
<a href="<?php the_permalink() ?>"><?php the_post_thumbnail(); ?></a> 
<?php } else { ?> 
<?php } ?>  
<!--end thumbnail code--> 
<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2> 
<p class="smallprint">Posted on <?php the_time('F jS, Y') ?> <!-- by <?php the_author() ?> --></p> 
<!-- shows excerpts below --> 
<?php 
global $more; 
$more = 0; 
?>  
<!-- end shows excerpts code -->             
<?php the_content('Read the rest of this entry &raquo;'); ?> 
<p class="post_footer"><?php the_tags('Tags: ', ', ', '<br />'); ?> Posted in <?php the_category(', ') ?> | <?php edit_post_link('Edit'); ?>  </p> 
</div> 
<?php } ?> <!-- close expiration function bracket before ending the loop --> 
<?php endwhile; endif; ?>


Thank you. That got it working.
Just for those who plan on using that code too, it does have it’s limitations. All posts for that page need to have an expiry date attached to them in the custom field. ( Custom field: expiration Value: mm/dd/yyyy hh:mm:ss ) If you leave out the expiration date on a post it will not show on the page at all.
If you have a fix for this let me know!
Also, I’m not exactly sure what time it is based on (I’m in a different time zone than the website owners) but that isn’t a problem for me since the posts don’t need to expire at an exact time.

To get the posts without expiration date set on them to display, do something like this:

<?php query_posts('cat=4,1'); ?> 

<?php if (have_posts()) : ?> 
<?php while (have_posts()) : the_post(); ?> 
<?php
    $currentdate = date("Ymd");
    $expirationdate = get_post_custom_values('expiration');
    if (is_null($expirationdate)) {
    $expirestring = '30005050'; // Set future date to not have other posts expire.
    } else {
     if (is_array($expirationdate)) {
    $expirestringarray = implode($expirationdate);
    }
    $expirestring = str_replace("/","",$expirestringarray);
    } //else
    if ( $expirestring > $currentdate ) { ?>

<div <?php post_class() ?> id="post-<?php the_ID(); ?>"> 
<!-- thumbnail code below --> 
<?php if ( has_post_thumbnail() ) { ?> 
<a href="<?php the_permalink() ?>"><?php the_post_thumbnail(); ?></a> 
<?php } else { ?> 
<?php } ?>  
<!--end thumbnail code--> 
<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2> 
<p class="smallprint">Posted on <?php the_time('F jS, Y') ?> <!-- by <?php the_author() ?> --></p> 
<!-- shows excerpts below --> 
<?php 
global $more; 
$more = 0; 
?>  
<!-- end shows excerpts code -->             
<?php the_content('Read the rest of this entry &raquo;'); ?> 
<p class="post_footer"><?php the_tags('Tags: ', ', ', '<br />'); ?> Posted in <?php the_category(', ') ?> | <?php edit_post_link('Edit'); ?>  </p> 
</div> 
<?php } ?>
<?php endwhile; endif; ?>

I tried taking away the expiration date on the first post after inserting the new code and now it does the opposite - it’s only showing the post without the expiration date. :confused:

Not sure I get what you mean. You removed the custom field on a post and now it’s not displaying that post? Can you elaborate? :slight_smile:

I had four posts all appearing on my page and they all had expiration dates set. When I inserted the new code and tested it by deleting an expiration date, only that post appeared. The posts with the expiration dates were no longer appearing.

Change this line:

    $currentdate = date("Ymd");

to this:

    $currentdate = date("mdY");

You located the problem, I was entering the wrong string format in my custom field value. I was still entering it in mdY format when I should’ve been entering it Ymd according to your new code.

(Using mdY doesn’t work because then when I compare expirestring with currentdate it’s possible to have the string of a date next year, eg. 01012012, be less than one for this year eg. 12122011, and so because it doesn’t satisfy ($expirestring>$currentdate), the post wouldn’t show.)

Thanks for the help!

Hello,

i am also interested in this code but where to insert it?

Thanks in advance