SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Member
    Join Date
    Apr 2010
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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 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 exemple...   the_title();  
                the_excerpt();  
                }  
            endwhile;  
        endif;  
        ?>
    and I need to integrate it into my custom loop...
    PHP Code:
    <?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?

  2. #2
    Mazel tov! bronze trophy kohoutek's Avatar
    Join Date
    Aug 2004
    Location
    Hamburg, Germany
    Posts
    4,248
    Mentioned
    30 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:

    <?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 ) { ?><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; ?>
    Maleika E. A. | Rockatee | Twitter | Dribbble



  3. #3
    SitePoint Member
    Join Date
    Apr 2010
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  4. #4
    Mazel tov! bronze trophy kohoutek's Avatar
    Join Date
    Aug 2004
    Location
    Hamburg, Germany
    Posts
    4,248
    Mentioned
    30 Post(s)
    Tagged
    0 Thread(s)
    To get the posts without expiration date set on them to display, do something like this:

    PHP Code:
    <?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; ?>
    Maleika E. A. | Rockatee | Twitter | Dribbble



  5. #5
    SitePoint Member
    Join Date
    Apr 2010
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  6. #6
    Mazel tov! bronze trophy kohoutek's Avatar
    Join Date
    Aug 2004
    Location
    Hamburg, Germany
    Posts
    4,248
    Mentioned
    30 Post(s)
    Tagged
    0 Thread(s)
    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?
    Maleika E. A. | Rockatee | Twitter | Dribbble



  7. #7
    SitePoint Member
    Join Date
    Apr 2010
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  8. #8
    Mazel tov! bronze trophy kohoutek's Avatar
    Join Date
    Aug 2004
    Location
    Hamburg, Germany
    Posts
    4,248
    Mentioned
    30 Post(s)
    Tagged
    0 Thread(s)
    Change this line:

    PHP Code:
        $currentdate date("Ymd"); 
    to this:

    PHP Code:
        $currentdate date("mdY"); 
    Maleika E. A. | Rockatee | Twitter | Dribbble



  9. #9
    SitePoint Member
    Join Date
    Apr 2010
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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!

  10. #10
    SitePoint Member
    Join Date
    Jul 2013
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kohoutek View Post
    Change this line:

    PHP Code:
        $currentdate date("Ymd"); 
    to this:

    PHP Code:
        $currentdate date("mdY"); 

    Hello,

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

    Thanks in advance


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •