【问题标题】:Wordpress meta_query datetime operationsWordpress meta_query 日期时间操作
【发布时间】:2020-11-12 15:14:37
【问题描述】:

我想加载所有具有自定义字段('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
   ), 

【问题讨论】:

    标签: php mysql wordpress


    【解决方案1】:

    我建议在这里阅读更多内容: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
      )
    )
    

    【讨论】:

    • 谢谢。但是...您的答案解释了如何与今天进行比较,我想检查“date_flag”是否距其他日期 4 小时。
    • 那么与“现在”加四个小时比较?还是你有一些“其他”需要增加四个小时?
    • 已更新以包含更多示例。
    • 谢谢。一般是好的答案,但对我的目标来说它不是正确的答案。我想检查 'date_flag' 是否经过了 4 小时...您的解决方案检查 'date_flag' 是否在当前时间+4 之后...这样,它总是会给出相同的结果。如果'date_flag'是[2020-11-12 07:00:00],现在它的[2020-11-12 11:01:00] = true,如果'date_flag'是[2020-11-12 07:00]: 00] 现在它的 [2020-11-12 10:01:00] = false,
    猜你喜欢
    • 2015-01-01
    • 2021-03-18
    • 2012-05-14
    • 1970-01-01
    • 2013-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-22
    相关资源
    最近更新 更多