我从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\'
)
)
);
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