Skip to content

Instantly share code, notes, and snippets.

@ferourives
Last active August 1, 2024 23:45
Show Gist options
  • Save ferourives/53cc8bcfe4ab65bba599a9b0d97772e1 to your computer and use it in GitHub Desktop.
Save ferourives/53cc8bcfe4ab65bba599a9b0d97772e1 to your computer and use it in GitHub Desktop.
Event countdown WP loop with datepicker ACF
<!-- competitions countdown with post type "competição" -->
<?php
//If the event is happening
$args = array(
'post_type' => 'competicao',
'meta_query' => [
'relation' => 'AND',
[
'key' => 'date_picker',
'compare' => '<=',
'value' => $today->format('Ymd'),
],
[
'key' => 'date_picker_fim',
'compare' => '>=',
'value' => $today->format('Ymd'),
]
],
'meta_key' => 'date_picker',
'posts_per_page' => 1,
'orderby' => 'meta_value_num',
'order' => 'ASC'
);
$post_query = new WP_Query($args);
if ($post_query->have_posts()) { ?>
<div class="temporizador">
<?php while ($post_query->have_posts()) : $post_query->the_post();
$today = new DateTime('today 00:00');
$date = DateTime::createFromFormat('Ymd', get_field('date_picker'));
$intervalo = $today->diff($date); ?>
<div class="temp">
<i class="material-icons">&#xE8B5;</i> Acontecendo:
</div>
<div class="text">
<b> <?php the_title(); ?></b>
</div>
<a href="<?php echo the_permalink(); ?>" class="btn">Saiba Mais</a>
<?php endwhile; ?>
</div>
<?php } else {
//If the event will happen
$args = array(
'post_type' => 'competicao',
'meta_query' => [
['key' => 'date_picker', 'value' => $today->format('Ymd'), 'compare' => '>='],
],
'meta_key' => 'date_picker',
'posts_per_page' => 1,
'orderby' => 'meta_value_num',
'order' => 'ASC'
);
$post_query = new WP_Query($args);
if ($post_query->have_posts()) { ?>
<div class="temporizador">
<?php while ($post_query->have_posts()) : $post_query->the_post();
$today = new DateTime('today 00:00');
$date = DateTime::createFromFormat('Ymd', get_field('date_picker'));
$intervalo = $today->diff($date); ?>
<div class="temp">
<i class="material-icons">&#xE8B5;</i> <?php echo "{$intervalo->days} dias"; ?>
</div>
<div class="text">
To next event: <b> <?php the_title(); ?></b>
</div>
<?php endwhile; ?>
</div>
<?php }
} ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment