Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Events list — event not show in loop if past end date WordPress

  • SOLVED

I have an events list that lists all upcoming events, I need to make it so that events are not shown if the event end date (meta_event_end_date) has gone past midnight on todays date.

<strong>Example</strong>
An event might have:

— A start date (meta_event_date) of 24th August
— End date (meta_event_end_date) of 28th August.
— The event should not get displayed in the loop if the date is 29th August.
— But still be displayed if the date is the 25th August

I tried using 'meta_compare', but couldn't get it to properly work…

<strong>Code</strong>


<div class="home-events">
<h3>Latest Events <a href="/noticeboard/" class="more-small">more</a></h3>
<ul class="event-list">
<?php
global $post;
$tmp_post = $post;
$args = array('numberposts' => 3, 'post_type'=> 'event_post', 'category'=> '-16', 'meta_key'=> 'meta_event_date', 'orderby' => 'meta_value', 'order' => 'ASC' );
$myposts = get_posts( $args );

foreach($myposts as $post) :
setup_postdata($post);
$meta_event_date = strtotime( get_post_meta( get_the_ID(), 'meta_event_date', true ) );

?>

<li class="event clearfix">
<a href="<?php the_permalink() ?>" title="<?php the_title(); ?>">
<div class="date">
<div class="number"><?php echo date('d', $meta_event_date) ?></div>
<div class="month"><?php echo date('M', $meta_event_date) ?></div>
</div>
<h3><?php the_title(); ?></h3>
</a>
</li>

<?php endforeach; ?>

<?php $post = $tmp_post; ?>

</ul>

</div>

Answers (4)

2012-08-24

Pavel Petrov answers:

Here is the working solution :) :


<?php
global $post;
$tmp_post = $post;
$args = array('numberposts' => 99, 'post_type'=> 'event_post', 'category'=> '-16', 'meta_key'=> 'meta_event_date', 'orderby' => 'meta_value', 'order' => 'ASC', );
$myposts = get_posts( $args );

foreach($myposts as $post) :
setup_postdata($post);
$meta_event_date = strtotime( get_post_meta( get_the_ID(), 'meta_event_date', true ) );
$meta_event_end_date = strtotime( get_post_meta( get_the_ID(), 'meta_event_end_date', true ) );
$start = date("y", $meta_event_date)."-".date("m", $meta_event_date)."-".date("d", $meta_event_date)." ".date("H", $meta_event_date).":".date("i", $meta_event_date);
$end = date("y", $meta_event_end_date)."-".date("m", $meta_event_end_date)."-".date("d", $meta_event_end_date)." ".date("H", $meta_event_end_date).":".date("i", $meta_event_end_date);
$current = date("y")."-".date("m")."-".date("d")." ".date("H").":".date("i");
if(strtotime($current) >= strtotime($start) && strtotime($current) <= strtotime($end))
{
?>

<li class="event clearfix">
<a href="<?php the_permalink() ?>" title="<?php the_title(); ?>">
<div class="date">
<div class="number"><?php echo date('d', $meta_event_date) ?></div>
<div class="month"><?php echo date('M', $meta_event_date) ?></div>
</div>
<h3><?php the_title(); ?></h3>
</a>
</li>
<?php } ?>
<?php endforeach; ?>

2012-08-24

Gabriel Reguly answers:

Hi Nick,

Can you post the output of


echo get_post_meta( get_the_ID(), 'meta_event_date', true );


Regards,
Gabriel


Nick comments:

Hi Gabriel, here is the meta box function

array(
'name' => 'Start Date',
'id' => "meta_event_date",
'type' => 'date',
'format' => 'd MM, yy'
),
array(
'name' => 'End Date',
'id' => "meta_event_end_date",
'type' => 'date',
'format' => 'd MM, yy'
)
)


Gabriel Reguly comments:

Ok, please bear with me while I code a solution for you.


Gabriel Reguly comments:

Please try this code

<div class="home-events">
<h3>Latest Events <a href="/noticeboard/" class="more-small">more</a></h3>
<ul class="event-list">
<?php
global $post;
$tmp_post = $post;
$args = array('numberposts' => 3, 'post_type'=> 'event_post', 'category'=> '-16', 'meta_key'=> 'meta_event_date', 'orderby' => 'meta_value', 'order' => 'ASC' );
$myposts = get_posts( $args );
$today = date('YMD');
foreach($myposts as $post) :
setup_postdata($post);
$meta_event_date = strtotime( get_post_meta( get_the_ID(), 'meta_event_date', true ) );
if ( $today > date('YMD', $meta_event_date) ) {
continue;
}
?>
<li class="event clearfix">
<a href="<?php the_permalink() ?>" title="<?php the_title(); ?>">
<div class="date">
<div class="number"><?php echo date('d', $meta_event_date) ?></div>
<div class="month"><?php echo date('M', $meta_event_date) ?></div>
</div>
<h3><?php the_title(); ?></h3>
</a>
</li>
<?php endforeach; ?>
<?php $post = $tmp_post; ?>
</ul>
</div>


Nick comments:

Hi Gabriel,

That doesn't seem to work.

I have an event that:

start date: 22 August, 2012
end date: 23 August, 2012

And it still is showing in my events list with your code.

Should you be filtering by meta_event_end_date?


Gabriel Reguly comments:

Oh dear, yes.

Of course it should be the end date, apologies.

Please try the amended code below.


<div class="home-events">
<h3>Latest Events <a href="/noticeboard/" class="more-small">more</a></h3>
<ul class="event-list">
<?php
global $post;
$tmp_post = $post;
$args = array('numberposts' => 3, 'post_type'=> 'event_post', 'category'=> '-16', 'meta_key'=> 'meta_event_date', 'orderby' => 'meta_value', 'order' => 'ASC' );
$myposts = get_posts( $args );
$today = date('YMD');
foreach($myposts as $post) :
setup_postdata($post);
$meta_event_date = strtotime( get_post_meta( get_the_ID(), 'meta_event_date', true ) );
$meta_event_end_date = strtotime( get_post_meta( get_the_ID(), 'meta_event_end_date', true ) );
if ( $today > date('YMD', $meta_event_end_date) ) {
continue;
}
?>
<li class="event clearfix">
<a href="<?php the_permalink() ?>" title="<?php the_title(); ?>">
<div class="date">
<div class="number"><?php echo date('d', $meta_event_date) ?></div>
<div class="month"><?php echo date('M', $meta_event_date) ?></div>
</div>
<h3><?php the_title(); ?></h3>
</a>
</li>
<?php endforeach; ?>
<?php $post = $tmp_post; ?>
</ul>
</div>


Gabriel Reguly comments:

Any luck?

I am going offline soon.

Tomorrow I can help you again if needed.

Regards,
Gabriel


Nick comments:

Hi Gabriel,

The older event still shows in the loop , any ideas?

Nick

2012-08-24

Arnav Joy answers:

try this

<div class="home-events">

<h3>Latest Events <a href="/noticeboard/" class="more-small">more</a></h3>

<ul class="event-list">

<?php

global $post;

$tmp_post = $post;

$args = array('numberposts' => 3, 'post_type'=> 'event_post', 'category'=> '-16', 'meta_key'=> 'meta_event_date', 'orderby' => 'meta_value', 'order' => 'ASC' );

$myposts = get_posts( $args );

$today = date('d-m-Y');

foreach($myposts as $post) :

setup_postdata($post);

$meta_event_date = strtotime( get_post_meta( get_the_ID(), 'meta_event_date', true ) );

$meta_event_end_date = strtotime( get_post_meta( get_the_ID(), 'meta_event_end_date', true ) );

if ( strtotime($today) > date('d-m-Y', strtotime($meta_event_end_date)) ) {

continue;

}

?>

<li class="event clearfix">

<a href="<?php the_permalink() ?>" title="<?php the_title(); ?>">

<div class="date">

<div class="number"><?php echo date('d', $meta_event_date) ?></div>

<div class="month"><?php echo date('M', $meta_event_date) ?></div>

</div>

<h3><?php the_title(); ?></h3>

</a>

</li>

<?php endforeach; ?>

<?php $post = $tmp_post; ?>

</ul>

</div>


Nick comments:

Hi Arnav,

That doesn't seem to work, it makes all events disappear

Nick


Arnav Joy comments:

replace this line


if ( strtotime($today) > date('d-m-Y', strtotime($meta_event_end_date)) )

with following line

if ( $today > date('d-m-Y', strtotime($meta_event_end_date)) )


Nick comments:

Still the same result unfortunately, any ideas?


Arnav Joy comments:

try this

<div class="home-events">



<h3>Latest Events <a href="/noticeboard/" class="more-small">more</a></h3>



<ul class="event-list">



<?php



global $post;



$tmp_post = $post;



$args = array('numberposts' => 3, 'post_type'=> 'event_post', 'category'=> '-16', 'meta_key'=> 'meta_event_date', 'orderby' => 'meta_value', 'order' => 'ASC' );



$myposts = get_posts( $args );



$today = date('d MM, yy');



foreach($myposts as $post) :



setup_postdata($post);



$meta_event_date = strtotime( get_post_meta( get_the_ID(), 'meta_event_date', true ) );



$meta_event_end_date = strtotime( get_post_meta( get_the_ID(), 'meta_event_end_date', true ) );



if ( strtotime($today) > strtotime($meta_event_end_date) ) {



continue;



}



?>



<li class="event clearfix">



<a href="<?php the_permalink() ?>" title="<?php the_title(); ?>">



<div class="date">



<div class="number"><?php echo date('d', $meta_event_date) ?></div>



<div class="month"><?php echo date('M', $meta_event_date) ?></div>



</div>



<h3><?php the_title(); ?></h3>



</a>



</li>



<?php endforeach; ?>



<?php $post = $tmp_post; ?>



</ul>



</div>


Nick comments:

Events are back but the expired event is showing still

http://grfk.co.nz/GVU3

2012-08-24

Daniel answers:

Does the expired event get a class attached to it or a category? How does it happen? Does a user manually set events to Expired category/tag or is it based on time?


Nick comments:

Hi Daniel,

When they create an event they set a start and end date.

There is no 'expiry', I'm just trying to hide old events via the loop

Cheers


Nick comments:

NB: there is no expiry as the client wants old events to show in the search results.


Daniel comments:

if ( strtotime($today) > strtotime($meta_event_end_date) ) {

continue;

}


When I'm looking at that IF statement from Arnav Joy (above), it's only checking for: IF future date is newer than the current end date, keep showing events. It code also needs to check for IF future date is older than or equal to the end date, then hide events or apply a class and hide them with CSS.


Look at "2) query" section at the link below to think it out:
[[LINK href="http://www.noeltock.com/web-design/wordpress/how-to-custom-post-types-for-events-pt-2"]]http://www.noeltock.com/web-design/wordpress/how-to-custom-post-types-for-events-pt-2/[[/LINK]]


Daniel comments:

Or when applying the class, the CSS could grey out the font color a bit to show they had expired.


Nick comments:

We could hide with css, though I'd prefer the loop not to show them at all.

Using CSS seems like an unnecessary step if all we need to do is get the query right.


Nick comments:

And all I am trying to do is compare todays date with two other dates ( event start and event end)