Home Forums General Issues weeknumber for events in the current week


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 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(
    	        '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 ): ?>
    				<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> 
    		<?php endforeach; ?>
  • 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.

Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘weeknumber for events in the current week’ is closed to new replies.