Support

Account

Home Forums General Issues Show only upcoming events from current month

Solved

Show only upcoming events from current month

  • Hello. I have this code:

    <?php
        $today = date('Ymd');
        $args = array(
                    'posts_per_page'  => 7,
            'post_type'     => 'competicions',
            'orderby'       => 'meta_value',
                    'order'       => 'asc',
            'meta_key'      => 'datahora_prova', //ACF date field
            'meta_query'    => array( array(
                'key' => 'datahora_prova', 
                'value' => $today, 
                'compare' => '>=',
                'type' => 'DATE'
            ))
        );
        $upcoming_events = new WP_Query( $args );
        if ( $upcoming_events->have_posts() ) :
    ?>
    
    <?php while ( $upcoming_events->have_posts() ) : $upcoming_events->the_post(); ?>
    
    <div class="widget_next_competicions_title">
    <?php $permalink = get_permalink(); the_title('<a href="' . $permalink . '">', '</a>'); ?>
    </div>
    <div class="widget_next_competicions_date">
    <?php echo get_field('datahora_prova'); ?>
    </div>
    
    <?php endwhile; wp_reset_postdata(); ?>
    
    <?php endif; ?>

    How can I set it to only show the upcoming events but only in the current month? Now it shows all the upcoming events.

    Thanks.

  • The first problem you have is that you can’t use “Date” as the meta type because ACF date fields are not stored as “dates” in the database. the type needs to be strings or numbers.

    date('Ymt') will return the last day of the month for a given date.

    
    <?php
      $today = date('Ymd');
      $last_of_month = date('Ymt', strtotime(date('Y-m-d')));
      $args = array(
        'posts_per_page'  => 7,
        'post_type' => 'competicions',
        'orderby' => 'meta_value',
        'order' => 'asc',
        'meta_key' => 'datahora_prova', //ACF date field
        'meta_query' => array(
          array(
            'key' => 'datahora_prova', 
            'value' => $today, 
            'compare' => '>=',
          ),
          array(
            'key' => 'datahora_prova', 
            'value' => $last_of_month, 
            'compare' => '<=',
          )
        )
      );
    
  • Thank you, but the code returns me nothing. Have I miss something?

    <?php
      $today = date('Ymd');
      $last_of_month = date('Ymt', strtotime(date('Y-m-d')));
      $args = array(
        'posts_per_page'  => 7,
        'post_type' => 'competicions',
        'orderby' => 'meta_value',
        'order' => 'asc',
        'meta_key' => 'datahora_prova', //ACF date field
        'meta_query' => array(
          array(
            'key' => 'datahora_prova', 
            'value' => $today, 
            'compare' => '>=',
          ),
          array(
            'key' => 'datahora_prova', 
            'value' => $last_of_month, 
            'compare' => '<=',
          )
        )
      );
        $upcoming_events = new WP_Query( $args );
        if ( $upcoming_events->have_posts() ) :
    ?>
    
    <?php while ( $upcoming_events->have_posts() ) : $upcoming_events->the_post(); ?>
    
    <div class="widget_next_competicions_title">
    <?php $permalink = get_permalink(); the_title('<a href="' . $permalink . '">', '</a>'); ?>
    </div>
    <div class="widget_next_competicions_date">
    <?php echo get_field('datahora_prova'); ?>
    </div>
    
    <?php endwhile; wp_reset_postdata(); ?>
    
    <?php endif; ?>
  • I may have missed something but I can’t see any reason it’s not working.

  • The code I posted before (your code + the rest of “mine”) I understand that it’s correct, right?

  • yes, it looks right. Like I said, I don’t see anything wrong with it. From what I know it should be working.

    Before doing the query add

    
    echo '<pre>'; print_r($args); echo '</pre>';
    

    to double check what it actually looks like and if it’s right.

  • It returns this:

    Array
    (
        [posts_per_page] => 7
        [post_type] => competicions
        [orderby] => meta_value
        [order] => asc
        [meta_key] => datahora_prova
        [meta_query] => Array
            (
                [0] => Array
                    (
                        [key] => datahora_prova
                        [value] => 20220706
                        [compare] => >=
                    )
    
                [1] => Array
                    (
                        [key] => datahora_prova
                        [value] => 20220731
                        [compare] => <=
                    )
    
            )
    
    )
  • I don’t see anything wrong with those query args or a reason it should not be working.

  • The datahora_prova has this format: d/m/Y | H:i. Can be this the problem?
    Thanks for the help.

  • The return format is not how it is stored in the DB.

    Are you using a date field or a date/time field?

    this in your OP indicated to me that you are using a date field $today = date('Ymd');.

    But the format on your last posts indicates a date/time field.

    Which is it?

  • It’s a date-time field.

  • 
    <?php
      $today = date('Y-m-d');
      $last_of_month = date('Y-m-t');
      $args = array(
        'posts_per_page'  => 7,
        'post_type' => 'competicions',
        'orderby' => 'meta_value',
        'order' => 'asc',
        'meta_key' => 'datahora_prova', //ACF date field
        'meta_query' => array(
          array(
            'key' => 'datahora_prova', 
            'value' => $today, 
            'compare' => '>=',
            'type' => 'DATE'
          ),
          array(
            'key' => 'datahora_prova', 
            'value' => $last_of_month, 
            'compare' => '<=',
            'type' => 'DATE'
          )
        )
      );
    
  • Working, thanks!

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

You must be logged in to reply to this topic.