0

I have some problem with my custom post event. I would like to show only events with date is equal or greater than today, but my codes does not work properly and show all events from past and future.

My code:

Register Custom Post Type in functions.php

function create_posttype() {
register_post_type('event', array(
  'public' => true,
  'has_archive' => false,
  'taxonomies' => array('category'),
  'supports' => array(
        'title', 'editor','thumbnail', 'custom-fields', 'revisions',     'page-attributes', 'post-formats'
    ),
  'menu_icon' => 'dashicons-calendar-alt',
  'rewrite' => array('slug' => 'events'),

  'labels' => array(
  'name' => __( 'Events' ),
  'singular_name' => __( 'Event' ),
        'add_new_item' =>__('Add New Event'),
        'edit_item' =>__('Edit Event'),
        'all_item' =>__('All Events')
       ),
  ));
add_action( 'init', 'create_posttype' );

front-page.php where i want to show my future events:

start_event_date is a Custom Field (Date Picker), Display Format: d/m/Y, Return Format: d/m/Y,

 $today =  date('d/m/Y');
    $argsEvents = array(
     'post_type' => 'event',
     'posts_per_page' => 3,
     'meta_key' => 'start_event_date',
     'orderby' => 'meta_value',
     'order' => 'ASC',
     'meta_query' => array(
         array(
           'key' => 'start_event_date',
            'compare' => '>=',
            'value' => $today,
             'type' => 'datetime'
           )
       )                    
     );

$theEventsPosts = new WP_Query($argsEvents);
    while($theEventsPosts->have_posts()){
      $theEventsPosts->the_post();

//here i have my event-card-post html 
}

I think that everthing with my code is correct but still does not work. Please give me any advice.

ceki10
  • 91
  • 1
  • 13

1 Answers1

0

Recently I had the same issue. When I digged into the ACF PRO docs, I found out that the return date format has nothing to do with how the field's date is stored in the database. In fact, it is Ymd - https://www.advancedcustomfields.com/resources/date-picker/ and this is the value you should use for your $today. Also, I used type 'DATE' in the meta_query.

$today =  date('Ymd');

and

'meta_query' => array(
  array(
    'key' => 'start_event_date',
    'compare' => '>=',
    'value' => $today,
    'type' => 'DATE'
  )
)