簡體   English   中英

Wordpress meta_query 日期時間操作

[英]Wordpress meta_query datetime operations

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM