我有一些贴子有一些自定义字段,其中我有一个字符串格式的日期,所以我循环我的贴子,并将它们转换为日期,然后我做了一些事情按日期排列:
foreach($posts as $post) {
$postid = $post->ID;
$myDate = \DateTime::createFromFormat('j-n-Y', get_post_meta($postid, 'usp-custom-80', true));
$postOrdered[$postid] = \DateTime::createFromFormat('j-n-Y', get_post_meta($postid, 'usp-custom-80', true));
wp_reset_query();
}
arsort($postOrdered);
var_export($postOrdered);
所以我把日期和身份证一起附上,这给了我们:
array ( 128288 => DateTime::__set_state(array( 'date' => '2017-08-20 20:36:02.000000', 'timezone_type' => 3, 'timezone' => 'UTC', )), 128166 => DateTime::__set_state(array( 'date' => '2017-08-17 20:36:02.000000', 'timezone_type' => 3, 'timezone' => 'UTC', )), 128308 =>
DateTime::__set_state(array( 'date' => '2017-08-05 20:36:02.000000', DateTime::__set_state(array( 'date' => '2000-03-20 20:36:02.000000',
DateTime::__set_state(array( 'date' => '1978-05-11 20:36:02.000000', 'timezone_type' => 3, 'timezone' => 'UTC', )), 128295 =>
DateTime::__set_state(array( 'date' => '1978-04-10 20:36:02.000000', 'timezone_type' => 3, 'timezone' => 'UTC', )), 128337 =>
DateTime::__set_state(array( 'date' => '1978-03-16 20:36:02.000000', 'timezone_type' => 3, 'timezone' => 'UTC', )), 128315 => DateTime::__set_state(array( 'date' => '1976-08-10 20:36:02.000000', 'timezone_type' => 3, 'timezone' => 'UTC', )), 128290 => DateTime::__set_state(array( 'date' => '1970-04-12 20:36:02.000000', 'timezone_type' => 3, 'timezone' => 'UTC', )), )
好了,现在我们要做的是
得到
大约$start和$end日期
form
使用
GET
$start = $_GET['start'];
$end = $_GET['end'];
$start = \DateTime::createFromFormat('j-n-Y', $start);
$end = \DateTime::createFromFormat('j-n-Y', $end);
var_dump($start);
var_dump($end);
这给了我们:
object(DateTime)#8267 (3) { ["date"]=> string(26) "2019-02-01 20:51:56.000000" ["timezone_type"]=> int(3) ["timezone"]=> string(3) "UTC" }
object(DateTime)#7974 (3) { ["date"]=> string(26) "2019-02-05 20:51:56.000000" ["timezone_type"]=> int(3) ["timezone"]=> string(3) "UTC" }
现在我要做的是简单检查这些帖子中是否有任何一个在$start和$end之间的日期,并显示它们order asc或desc
字面意思是
if ( ( $myDate >= $start) && ( $myDate <= $end) ) {
...my posts...
但这仍然给了我们所有的职位