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())) { ?>
Lew Ayotte answers:
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()) { ?>
cdogstu99 comments:
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
Lew Ayotte comments:
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
cdogstu99 comments:
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.
Lew Ayotte comments:
Try changing the && is_archive() back to || is_archive()...
cdogstu99 comments:
NICE!!! Thanks for hangin in there with me! That did it! WOO HOO
Rashad Aliyev answers:
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;
?>
Rashad Aliyev comments:
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,
Rashad Aliyev comments:
Custom field name must be <strong>expiration</strong> for my suggestion. That's working..
cdogstu99 comments:
Rashad..tried this doesn't seem to be working for me.
Darrin Boutote answers:
Is the actual publish date of the post set to 2011 as well? If so, WP won't show the post.
cdogstu99 comments:
Nope ..2010
Buzu B answers:
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?
cdogstu99 comments:
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"
Buzu B comments:
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.