問題描述
我在 WP 模板中有一個 query_posts 調用。通過使用更多字段插件,我可以讓網站管理員創建一個事件 (自定義帖子類型),然後輸入格式為日期的日期:YYYY /mm /dd 。
主要問題是我應該傳遞什麼值到 meta_query 數組中的值選項?我目前正試圖通過”date(“Y /m /d h:i A”)”(減去引號),因為據瞭解,這將打印當前日期。我不在乎日期的時間,所以這可能是無關緊要的。 Ulitimatly 我試圖使用比較選項來確定即將到來的事件,過去的事件在這個網站的不同的地方。在另一個地方,我實際上需要傳遞值選項一個打印當月的第一天和最後一天的數組,將輸出限制在本月發生的事件中。
<?php
query_posts( array(
'post_type' => 'event', // only query events
'meta_key' => 'event_date', // load up the event_date meta
'orderby' => 'meta_value', // sort by the event_date
'order' => 'asc', // ascending, so earlier events first
'posts_per_page' => '2',
'meta_query' => array( // restrict posts based on meta values
'key' => 'event_date', // which meta to query
'value' => date("Y/m/d h:i A"), // value for comparison
'compare' => '>=', // method of comparison
'type' => 'DATE' // datatype, we don't want to compare the string values
) // end meta_query array
) // end array
); // close query_posts call
?>
最佳解決方案
我結束了同樣的事情,這篇文章是非常有幫助的。我使用自定義字段,這裏是我用來創建大於當前日期的所有事件的列表的代碼。注意額外的基於分類法的過濾器。
<?php // Let's get the data we need to loop through below
$events = new WP_Query(
array(
'post_type' => 'event', // Tell WordPress which post type we want
'orderby' => 'meta_value', // We want to organize the events by date
'meta_key' => 'event-start-date', // Grab the "start date" field created via "More Fields" plugin (stored in YYYY-MM-DD format)
'order' => 'ASC', // ASC is the other option
'posts_per_page' => '-1', // Let's show them all.
'meta_query' => array( // WordPress has all the results, now, return only the events after today's date
array(
'key' => 'event-start-date', // Check the start date field
'value' => date("Y-m-d"), // Set today's date (note the similar format)
'compare' => '>=', // Return the ones greater than today's date
'type' => 'DATE' // Let WordPress know we're working with date
)
),
'tax_query' => array( // Return only concerts (event-types) and events where "songs-of-ascent" is performing
array(
'taxonomy' => 'event-types',
'field' => 'slug',
'terms' => 'concert',
),
array(
'taxonomy' => 'speakers',
'field' => 'slug',
'terms' => 'songs-of-ascent',
)
)
)
);
?>
次佳解決方案
它在很大程度上取決於您的日期如何存儲在元數據中。一般來説,將 MySQL 的日期存儲為 MySQL 日期/時間戳是個好主意。
MySQL 時間戳格式為 Y-m-d h:i:s 。
但是,使用 WP 自己的日期管理函數總是一個好主意。因此,要以 MySQL 格式獲取當前日期,請使用 current_time('mysql')。
要格式化 MySQL 日期進行顯示,請使用 mysql2date($format, $mysql_date)。在這種情況下,最好在設置中顯示配置的日期,因此請使用 $format = get_option('date_format'); 。
要存儲 user-selected 日期,您必須將其轉碼為 MySQL 日期。為此,date('Y-m-d h:i:s', $unix_timestamp); 最簡單但不是最安全的方式。 $unix_timestamp 通常可以通過 strtotime($user_input)得到。
但是,strtotime()沒有對自己的身體進行健全檢查,所以最好編寫自己的收斂功能。
至於獲取月份範圍,我使用的功能是獲取任何 MySQL 時間戳的月份邊界:
function get_monthrange($time) {
$ym = date("Y-m", strtotime($time));
$start = $ym."-01";
$ym = explode("-", $ym);
if ($ym[1] == 12) {
$ym[0]++; $ym[1] = 1;
} else {
$ym[1]++;
}
$d = mktime( 0, 0, 0, $ym[1], 1, $ym[0] );
$d -= 86400;
$end = date("Y-m-d", $d);
return array( $start, $end );
}
如果你想獲得周界,WP 已經有了一個功能:get_weekstartend($time);,它也將邊界作為一個數組。
然後,您可以通過進行兩次單獨的比較,在 meta_query 參數中使用這些參數。
第三種解決方案
我結束了以下的事情。我設置了一個 event-momth 字段並從中進行比較。謝謝您的幫助
<?php
$event_query = new WP_Query(
array(
'post_type' => 'event', // only query events
'meta_key' => 'event-month', // load up the event_date meta
'order_by' => 'event_date',
'order' => 'asc', // ascending, so earlier events first
'meta_query' => array(
array( // restrict posts based on meta values
'key' => 'event-month', // which meta to query
'value' => date("n"), // value for comparison
'compare' => '=', // method of comparison
'type' => 'NUMERIC' // datatype, we don't want to compare the string values
) // meta_query is an array of query ites
) // end meta_query array
) // end array
); // close WP_Query constructor call
?>
<?php while($event_query->have_posts()): $event_query->the_post(); //loop for events ?>
參考文獻
注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。