Meta Query Compare with Date

时间:2021-02-01 作者:Florian

            $metas[] = array(
                \'key\' => \'event_date\',
                \'value\' => array(date(\'10/10/2020\'), date(\'10/11/2020\')),
                \'compare\' => \'BETWEEN\',
                \'type\' => \'DATE\'
你能告诉我这里怎么了吗?我怎样才能比较两次约会?

谢谢

比较的全部功能是:

    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;
}
我尝试了一些东西,比如&;日期自=2017年12月14日;dateuntil=20201217和(&;日期自=20171214;dateuntil=2020-12-17,有很多东西要展示,但他没有找到一个。

1 个回复
SO网友:RevNoah

看起来是日期格式。我用2020年10月10日(YYYY/MM/DD)的格式进行了尝试,效果如预期。您还可以使用连字符作为分隔符(YYYY-MM-DD),或完全忽略它们。由于您已经格式化了日期,因此不需要使用date()函数。

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

相关推荐

Comparison operator

我构建了一个下拉框,其中包含一系列比较字符串。array( \'value\' => array( __( \'Equal\', \'js_composer\' ) => \'=\', __( \'Does not equal\', \'js_composer\' ) => \'!=\', __( \'Greater than\', \'js_composer\' )