如何使用带有两个元值的“BETWING”执行wp_查询?

时间:2013-10-30 作者:Hor

我从Wordpress开始,已经实现了一些查询,但这一个。。。啊!这对我来说是一种痛苦!

我想做的是查询的这一部分:

" ... WHERE "VALUE_THAT_I_WANT" BETWEEN event_start_date AND event_end_date ..."
其中,“event\\u start\\u date”和“event\\u end\\u date”是元键(我想使用它们的值,而不是我的值!)

这就是我到目前为止所拥有的。。

$tEA = array(
    \'post_type\' => \'event\',
    \'meta_key\' => $metaKey,
    \'orderby\' => \'meta_value\',
    \'order\' => $order,
    \'posts_per_page\' => $postPerPage,
    \'meta_query\' => array(
        array(
            \'key\' => $metaKey,
            \'value\' => $today,
            \'compare\' => "=",
            \'type\' => \'DATE\'
        ),
        array(
            \'key\' => \'_event_end_time\'
        )
    )
);

3 个回复
SO网友:Ahmad M

这应该适合您:

$tEA = array(
    \'post_type\' => \'event\',
    // you dont need meta_key as you use meta_query
    //\'meta_key\' => $metaKey,
    \'orderby\' => \'meta_value\',
    \'order\' => $order,
    \'posts_per_page\' => $postPerPage,
    \'meta_query\' => array(
        array(
            \'key\' => $metaKey,
            // value should be array of (lower, higher) with BETWEEN
            \'value\' => array(\'START_DATE\', \'END_DATE\'),
            \'compare\' => \'BETWEEN\',
            \'type\' => \'DATE\'
        ),
    )
);
请注意,START\\u DATE和END\\u DATE应为MySQL日期格式YYYY-MM-DD

SO网友:Toni Michel Caubet

没有test 是的,但是这个呢?

$tEA = array(
    \'post_type\' => \'event\',
    \'meta_key\' => $metaKey,
    \'orderby\' => \'meta_value\',
    \'order\' => $order,
    \'posts_per_page\' => $postPerPage,
    \'meta_query\' => array(
        array(
            \'key\' => \'event_start_date\',
            \'value\' => \'VALUE_THAT_I_WANT\',
            \'compare\' => \'>=\'
        ),
        array(
            \'key\' => \'event_end_date\',
            \'value\' => \'VALUE_THAT_I_WANT\',
            \'compare\' => \'<=\'
        ),
        array(
            \'key\' => \'_event_end_time\'
        )
    )
);
我不确定你是否需要另一个比较,如果需要,只需预先准备:P

SO网友:Jules

由于WP 3.7,您可以将“date\\u query”作为参数传递。

从1月1日到2月28日返回帖子的示例:

$args = array(
    \'date_query\' => array(
        array(
            \'after\'     => \'January 1st, 2013\',
            \'before\'    => array(
                \'year\'  => 2013,
                \'month\' => 2,
                \'day\'   => 28,
            ),
            \'inclusive\' => true,
        ),
    ),
    \'posts_per_page\' => -1,
);
$query = new WP_Query( $args );
代码取自https://codex.wordpress.org/Class_Reference/WP_Query

结束

相关推荐

使用新的WP-Query()从循环中过滤后期格式;

嗨,我目前正在为我的博客构建一个主题。下面的代码指向最新的帖子(特色帖子)。因为这将有一个不同的风格比所有其他职位。然而我想过滤掉帖子格式:链接使用我在循环中定义的WP查询,因为它给我带来了更多的灵活性。我该怎么做呢? <?php $featured = new WP_Query(); $featured->query(\'showposts=1\'); ?> <?php while ($featured->have_post