Hi there!
I am a total newcomer to PHP. I have a wordpress site displaying events (posts) that are generated with with custom fields. I have not made the site but I’ve done some changes lately. The front page should show only upcoming events. The event nearest in time should show up first. It has been working well for long, but at some point it stopped working. Now I can only display events in DESC order and when I change to ASC the if-statement returns that there are no upcoming events. (For you who know wordpress: if I make an event “sticky” it will show up on top, but thats a temporary work around that only work for one post)
I have tried to deactivate plugins, with no luck. There is a chance it has to do with some major update of wordpress. Now I’m turning to you guys to see if you can see some obvious fault in the code. It’s probably not good code at all but it has been working well at least.
The if statement to display only upcoming events take the date and use strtotime, then adds a day to make sure the event shows the entire day it’s active. I have a feeling it has to do with how this conversion match the dates on the posts. But it’s weird that it display all events at DESC and none if I change to ASC… well…
Thanks a lot for any kind of help.
<h2>Upcoming events</h2>
<hr>
<?php
$args = array(
'numberposts' => -1,
'meta_key' => 'start_date',
'orderby' => 'meta_value',
'order' => 'DESC',
'meta_query' =>array(
array(
'key' => 'show_on_front_page',
'value' => '1'
)
)
);
$eventPosts = query_posts($args);
//print_r($eventPosts);
$didOutputEventPost = false; // display message about no upcoming events
foreach ($eventPosts as $post) {
$meta = get_fields($post->ID);
$date = $meta['start_date'];
$year = substr($date, 0, 4);
$month = ltrim(substr($date, 4, 2), '0');
$day = ltrim(substr($date, 6, 2), '0');
?>
<?php if(strtotime($date) + 86400 >= time()) :
$didOutputEventPost = true;
?>
<article id="post-<?php echo $event->ID ?>" <?php post_class(); ?>> <!-- start of post -->
<span class="post-title-wrapper"><span class="post-title"><a href="<?php echo get_permalink($event->ID) ?>" rel="bookmark" title="<?php printf(__('Link to %s', 'responsive'), the_title_attribute('echo=0')); ?>"><?php echo get_the_title($event->ID); ?></a></span></span><br/>
<span class="icn date"><?php echo substr($meta['start_date'], 0, 4); ?>-<?php echo substr($meta['start_date'], 4, 2); ?>-<?php echo substr($meta['start_date'], 6, 2); ?>
<?php
if(isset($meta['end_date']) && $meta['end_date'] > $meta['start_date'] ) { ?>
-
<?php echo substr($meta['end_date'], 0, 4); ?>-<?php echo substr($meta['end_date'], 4, 2); ?>-<?php echo substr($meta['end_date'], 6, 2); } ?>
</span>
<?php
if(isset($meta['start_time'])) { ?>
<span class="icn time">
<?php echo $meta['start_time']; } ?>
<?php
if(isset($meta['end_time'])) { ?>
-
<?php echo $meta['end_time']; } ?>
</span>
<span class="icn place">
<?php echo $meta['location']; ?><br/>
<?php echo $meta['postal_code']; ?> <?php echo $meta['city']; ?>
</span>
<?php if(isset($meta['price_non_members']) || isset($meta['price_members'])) { ?>
<span class="icn price">
<?php if(isset($meta['price_members'])){ echo 'Members: '. $meta['price_members']; } ?><br/>
<?php if(isset($meta['price_non_members'])){ echo 'Non-members: '. $meta['price_non_members']; } ?>
</span>
<?php } ?>
<div class="post-entry">
<a href="<?php echo get_permalink($event->ID) ?>" rel="bookmark" title="<?php printf(__('Link to %s', 'responsive'), the_title_attribute('echo=0')); ?>"><?php if(isset($meta['image'])) { ?>
<img class="event-image" src="<?php echo $meta['image']; ?>" width="150"/></a>
<?php } ?>
<?php $descr = substr($meta['description'],0,300).'...';
echo $descr; ?><br/>
<a href="<?php echo get_permalink($event->ID) ?>" rel="bookmark" title="<?php printf(__('Link to %s', 'responsive'), the_title_attribute('echo=0')); ?>">Read more</a>
</div> <!-- end of .post-entry -->
</article> <!-- end of .post -->
<?php endif; ?>
<?php } ?>
<?php if(!$didOutputEventPost) : ?>
<p>There are unfortunately no upcoming events to display at this moment, but hold on – soon we will announce a new moment to get together. In the meanwhile, make sure to sign up for the <a href="<?php echo esc_url(home_url('/')); ?>news/newsletter">newsletter</a> to ensure you don't miss out on the next one.</p>
<?php endif; ?>
</div><!-- end of #upcomingevents -->