php - Wordpress meta_query 日期时间操作
问题描述
我想加载具有自定义字段('date_flag')的所有产品的帖子(产品类型),如果存储在此自定义字段上的时间超过 4 小时(如果从存储日期过去 4 小时)此自定义字段)。
例如,如果“date_flag”是 2020-11-12 10:00:00,现在是 2020-11-12 14:30:00,那么给我这个帖子。
$args= array(
'post_type' => 'product',
'posts_per_page' => 5,
'meta_query' => array(
array(
'key' => 'date_flag',
'value' => what-to-do-here
'compare' => what-to-do-here
),
解决方案
我建议在这里阅读更多内容:https ://developer.wordpress.org/reference/classes/wp_meta_query/
// compare against now
array(
'key' => 'date_flag', // field to check
'value' => date("Y-m-d"), // today's date
'compare' => '<=', // your compare operator
'type' => 'DATE' // tell WP we're working with date
)
)
// compare against now + 4 hours
array(
'key' => 'date_flag', // field to check
'value' => date("Y-m-d H:i:s", strtotime('+4 hours')),
'compare' => '<=', // your compare operator
'type' => 'DATE' // tell WP we're working with date
)
)
// compare against some other date + 4 hours
$other_date = '2020-11-12 00:00:00';
$compare_date = date("Y-m-d H:i:s", strtotime('+4 hours', strtotime($other_date));
array(
'key' => 'date_flag', // field to check
'value' => $compare_date,
'compare' => '<=', // your compare operator
'type' => 'DATE' // tell WP we're working with date
)
)
推荐阅读
- c - 如何在scanf中要求未指定的输入?
- flutter - 动态更改图像背景
- java - 在 RCP 应用程序中对帮助内容的 IPv6 支持
- python - 如何将本地 csv 文件的内容保存到“硬编码”Pandas DataFrame 中?
- angular - Angular Gridster2:调整大小后如何触发项目中的功能?
- laravel - 如何更改登录页面的布局?
- python - 如何在python中对图像进行操作
- swift - URLSession 异步在 Swift 包中不起作用
- web - 我们的前端怎么知道这个http响应属于我们的请求呢?
- java - 在 Android 12 (SDK 31) 中获取 android.app.ForegroundServiceStartNotAllowedException