Meta Query Compare with Date

            $metas[] = array(
                'key' = 'event_date',
                'value' = array(date('10/10/2020'), date('10/11/2020')),
                'compare' = 'BETWEEN',
                'type' = 'DATE'

Can you tell me, whats wrong here? How can i realise an comparison between two dates?

Thanks!

The whole funktion for the comparison is:

    public function getReports($options = array())
{
    $reportsCPTName = ktkReports::getCustomPostName();
    $filiale = (isset($_REQUEST['setFiliale']) ? $_REQUEST['setFiliale'] : null);
    $kw = (isset($_REQUEST['setKW']) ? $_REQUEST['setKW'] : null);
    $kwyear = (isset($_REQUEST['setYEAR']) ? $_REQUEST['setYEAR'] : null);
    $datefrom = (isset($_REQUEST['datefrom']) ? $_REQUEST['datefrom'] : null);
    $dateuntil = (isset($_REQUEST['dateuntil']) ? $_REQUEST['dateuntil'] : null);
    if (isset($filiale)) {
        $metas[] = acf_reverse_meta('filiale', $filiale);
    }
    
    
    if (isset($datefrom)isset($dateuntil)){
        $metas[] = array(
            'key' = 'event_date',
            'value' = array($datefrom, $dateuntil),
            'compare' = 'BETWEEN',
            'type' = 'DATE'
        );
    } else if (isset($kw)) {
        if ($kw != '-1') {
            $kw = sprintf(%02d, $kw);
            $metas[] = array(
                'key' = 'KW',
                'value' = $kw,
                'compare' = '='
            );
        }
        if (isset($kwyear)) {
            $metas[] = array(
                'key' = 'KWY',
                'value' = $kwyear,
                'compare' = '='
            );
        } else {
            $metas[] = array(
                'key' = 'KWY',
                'value' = date('Y'),
                'compare' = '='
            );
        }
    }
    
    
    
    $args = array(
        'post_type' = $reportsCPTName,
        'orderby' = 'meta_value_num',
        'meta_key' = 'ktksort',
        'order' = 'DESC',
        'posts_per_page' = 250,
        'post_status' = 'publish'
    );
    if (isset($options['single'])  $options['single']) {
        $args['p'] = $options['single'];
    }
    switch ($this-getUserGroup()) {
        case ktkRechteUserGroup::Akzente:
            if (! isset($_REQUEST['allUnread'])  ! isset($options['allUnread'])  ! isset($options['single'])) {
                $metas[] = array(
                    'key' = 'kunde_id',
                    'value' = $_SESSION['kunde'],
                    'compare' = '='
                );
            }
            if (! isset($options['allOfKunde'])  ! isset($options['single'])) {
                $metas[] = array(
                    'key' = 'freigabe',
                    'value' = '0',
                    'compare' = '='
                );
            }
            if (! isset($options['stats'])) {
                $args['posts_per_page'] = 10;
                if ($this-reportPaged  0) {
                    $page_offset = ($this-reportPaged - 1) * $args['posts_per_page'];
                    $args['offset'] = $page_offset;
                }
            }
            break;
        case ktkRechteUserGroup::Kunde:
        case ktkRechteUserGroup::KundeVertrieb:
            if (isset($_REQUEST['allUnread']) || isset($options['allUnread'])) {
                $metas[] = array(
                    'key' = 'gesehen_kunde',
                    'value' = '0',
                    'compare' = '='
                );
            }
            $metas[] = array(
                'key' = 'kunde_id',
                'value' = $this-UserRechteObj-kunde-ID,
                'compare' = '='
            );
            $metas[] = array(
                'key' = 'freigabe',
                'value' = '1',
                'compare' = '='
            );
            if (! isset($options['stats'])) {
                $args['posts_per_page'] = 10;
                if ($this-reportPaged  0) {
                    $page_offset = ($this-reportPaged - 1) * $args['posts_per_page'];
                    $args['offset'] = $page_offset;
                }
            }
            break;
        case ktkRechteUserGroup::Freelancer:
            $fils = array();
            foreach ($this-UserRechteObj-filialen as $fil) {
                $fils[] = $fil-ID;
            }
            $metas[] = array(
                'key' = 'kunde_id',
                'value' = $_SESSION['kunde'],
                'compare' = '='
            );
            $metas[] = array(
                'key' = 'filiale_id',
                'value' = $fils,
                'compare' = 'in'
            );

            $admin_user_id_override = null;
            if (current_user_can('manage_options')) {
                $admin_user_id_override = (isset($_REQUEST['admin_user_id_override']) ? $_REQUEST['admin_user_id_override'] : null);
            }

            if ($admin_user_id_override) {
                $metas[] = array(
                    'key' = 'erfasser',
                    'value' = $admin_user_id_override,
                    'compare' = '='
                );
            } else {
                $metas[] = array(
                    'key' = 'erfasser',
                    'value' = get_current_user_id(),
                    'compare' = '='
                );
            }
            break;
    }
    if (isset($metas)) {
        $meta = buildMetaAnd($metas);
        $args = array_merge($meta, $args);
    }

    if (isset($options[search])) {
        add_filter('posts_where', array(
            $this,
            'title_filter'
        ), 10, 2);
        $args['search_for_text'] = $options[search];
        ktkDebug::ktkdeb('Content QS', $args);
        $q = query_posts($args);
        remove_filter('posts_where', array(
            $this,
            'title_filter'
        ), 10);
    } else {
        ktkDebug::ktkdeb('Content Q', $args);
        $q = query_posts($args);
    }

    ktkDebug::ktkdeb('getContentResult', count($q));
    return $q;
}

I tried stuff like datefrom=2017-12-14dateuntil=20201217 and datefrom=20171214dateuntil=2020-12-17 and there are a lot of things to show, but he did not find a single one.

Topic meta-query comparison Wordpress

Category Web


It looks like it is the date format. I tried it with the format 2020/10/10 (YYYY/MM/DD) and it worked as expected. You can also use hyphens as delimiters (YYYY-MM-DD), or omit them entirely. Since you've already formatted the date, you don't need to use the date() function.

$metas[] = array(
'key' => 'event_date',
'value' => array('2020/10/10', '2020/10/11'),
'compare' => 'BETWEEN',
'type' => 'DATE' );

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.