logo

$4
Expired Posts Code to Exclude: Problem with 2011 Date

I have code on my pages to exclude posts which have a particular deadline i have entered. The posts are archived and I enter the deadline as follows: 05/12/2010. I just tried to enter a deadline in 2011 with date 5/10/2011, and the post is not showing up so it seems to think it has expired. Maybe not recognizing the year? Any ideas why this might be happening?

The code looks like this:
<?php while(have_posts()) : the_post(); ?>
<?php //to check against expiration date;


$currentdate = date("mdY");
$expirationdate = get_post_custom_values('deadline');
if (is_null($expirationdate)) {
$expirestring = '50503000'; //MAKE UN-EXPIRING POSTS ALWAYS SHOW UP;
} else {

if (is_array($expirationdate)) {
$expirestringarray = implode($expirationdate);
}
$expirestring = str_replace("/","",$expirestringarray);
} //else

if (( $expirestring > $currentdate ) || (is_archive())) { ?>


cdogstu99 | 07/06/10 at 4:58pm | Edit


(4) Possible Answers Submitted...

  • avatar
    Last edited:
    07/06/10
    5:04pm
    Rashad Aliyev says:

    That's will help you..


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

    • 07/06/10 5:06pm

      Rashad Aliyev says:

      Edit your theme and change your loop.

      To create a post with date/time expiration, just create a custom field. Give it expiration as a key and your date/time (format: mm/dd/yyyy 00:00:00) as a value.

      The post will not show after that time stamp.

      best regards,

    • 07/06/10 5:08pm

      Rashad Aliyev says:

      Custom field name must be expiration for my suggestion. That's working..

    • 07/06/10 7:27pm

      cdogstu99 says:

      Rashad..tried this doesn't seem to be working for me.

  • avatar
    Last edited:
    07/06/10
    5:05pm
    Darrin Boutote says:

    Is the actual publish date of the post set to 2011 as well? If so, WP won't show the post.

    • 07/06/10 7:27pm

      cdogstu99 says:

      Nope ..2010

  • avatar
    Last edited:
    07/06/10
    5:11pm
    Buzu B says:

    well the problem is how you are doing the comparison.
    If i follow your code correctly, at the end you have two strings, one is for today:

    7062010

    and the other one is for the expiration date:

    5102011

    and then you compare to see which one is grates.

    if (( $expirestring > $currentdate )

    That is not a good way to do it. since, even though the year is bigger, the month isn't, so your expiration date is actually a smaller number. That is why you end up with your posts not showing up.

    A better way to do it is to work with dates directly or to firs compare the year, then the month and then the day. Now I see you are expecting an array as the value for your expiration date. What is the format of that array, and why an array?

    • 07/06/10 7:30pm

      cdogstu99 says:

      This kind of makes sense to me..unfortunately..i just took the code from here

      http://snipplr.com/view/3899/wordpress-post-expiration-code/

      and here

      http://snipplr.com/view/16400/updated-wordpress-post-expiration-code/

      and I altered the "Ymd" part to "mdY"

    • 07/06/10 10:46pm

      Buzu B says:

      The problem is the changes you made, since now the month appears first instead of the year. Like I said, I'd like to know the format of your array. If you want, I can just go and modify your code to make it work, just send me the login information for your wp-admin area, FTP credentials and the name of the file that has to be modified via a private message.

  • avatar
    Last edited:
    07/06/10
    11:07pm
    Lew Ayotte says:

    First, shouldn't your last line...

    if (( $expirestring > $currentdate ) || (is_archive())) { ?>


    be?

    if (( $expirestring > $currentdate ) && (is_archive())) { ?>




    Second,

    Try this for your date comparison:

    $currentdate  = strtotime(date("m/d/Y"));
    $expirationdate = strtotime(get_post_custom_values('deadline'));

    if ($expirationdate > $currentdate && is_archive()) { ?>

    • 07/06/10 8:10pm

      cdogstu99 says:

      This doesn't seem to be working. This is code i used

      <?php  //to check against expiration date;


      $currentdate = strtotime(date("m/d/Y"));

      $expirationdate = strtotime(get_post_custom_values('deadline'));
      if (is_null($expirationdate)) {
      $expirestring = '50503000'; //MAKE UN-EXPIRING POSTS ALWAYS SHOW UP;
      } else {

      if (is_array($expirationdate)) {
      $expirestringarray = implode($expirationdate);
      }
      $expirestring = str_replace("/","",$expirestringarray);
      } //else



      if ($expirationdate > $currentdate && is_archive()) { ?>


      and <?php } //end if for expiration; ?> before the <?php endwhile; ?>

      Here's error i get:

      Warning: strtotime() expects parameter 1 to be string, array given in

    • 07/06/10 9:54pm

      Lew Ayotte says:

      Oops... this is wrong:

      $expirationdate = strtotime(get_post_custom_values('deadline'));


      Try this:

      <?php  //to check against expiration date;

      $currentdate = strtotime(date("m/d/Y"));

      $expirationdate = get_post_custom_values('deadline');

      if (is_null($expirationdate)) {
      $expirestring = strtotime('5/5/3000'); //MAKE UN-EXPIRING POSTS ALWAYS SHOW UP;
      } else {
      if (is_array($expirationdate)) {
      $expirestringarray = implode($expirationdate);
      }
      $expirestring = strtotime($expirestringarray);
      }


      if ($expirestring > $currentdate && is_archive()) { ?>


      Lew

    • 07/06/10 10:04pm

      cdogstu99 says:

      Hey Lew,
      Tried that one..well, we got rid of the error message, but unfortunately now none of the posts are showing up. And i know they should be because i have some set with date in format 05/05/2011. I even tried 5/5/2011..but nothing still.

    • 07/06/10 10:22pm

      Lew Ayotte says:

      Try changing the && is_archive() back to || is_archive()...

    • 07/06/10 11:07pm

      cdogstu99 says:

      NICE!!! Thanks for hangin in there with me! That did it! WOO HOO

This question has expired.





Current status of this question: Completed