Home › Forums › General Issues › weeknumber for events in the current week
Hello, I am trying to display a number of events in a certain week. I use an advanced field for the startdate : “begindatum”
I adapted the code from here https://www.advancedcustomfields.com/resources/date-time-picker/ with a php snippet from somewhere on the internet which echoes the number of the week correctly.
However when I try to display an event in the current week nothing is showing up.
I think its a matter of finding the correct way to use the variable for $week in the query or maybe this is not the best way to do it this is the code I am using
$ddate = date('Y-m-d H:i:s');
$date = new DateTime($ddate);
$week = $date->format("W");
echo "Weeknummer: $week";
// query events
$posts = get_posts(array(
'posts_per_page' => -1,
'post_type' => 'any',
'meta_query' => array(
array(
'key' => 'begindatum',
'compare' => '=',
'value' => ($week),
'type' => 'DATE'
)
),
'order' => 'ASC',
'orderby' => 'meta_value',
'meta_key' => 'begindatum',
'meta_type' => 'DATE'
));
if( $posts ): ?>
<h2>Activiteiten in deze week</h2>
<ul id="events">
<?php foreach( $posts as $p ): ?>
<li>
<strong><?php echo $p->post_title; ?></strong>: <?php the_field('begindatum', $p->ID); ?> - <?php the_field('einddatum', $p->ID); ?>
<a href="
<?php echo get_site_url(); ?>/
<?php $post_type_data = get_post_type_object( $p->post_type ); $post_type_slug = $post_type_data->rewrite['slug']; echo $post_type_slug; ?>
">
<?php $obj = get_post_type_object( $p->post_type ); echo $obj->labels->singular_name; ?></a>
</li>
<?php endforeach; ?>
</ul>
ACF stores an SQL date. If we were talking about a straight MySQL query on the database you could do this. This can also be done with a date query, but this only works on the standard WP post date field.
There isn’t a way to use WP_Query() and meta_query to compare the dates in the a custom field against a week number. You need to get the first day of the week and the last day of the week and look for dates between these 2 values.
The topic ‘weeknumber for events in the current week’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.