2016-01-19 76 views
-2

我有一個START_YEAR和END_YEAR的WordPress:查詢年度年

FE一個範圍:項目-的Lorem-存有:$ START_YEAR = 2001,$ END_YEAR = 2005

,我需要選擇某年(fe $ filter_year = 2002)並顯示今年運行的帖子。 這意味着,$ filter_year可能是$ START_YEAR或$ END_YEAR或$ START_YEAR和$ END_YEAR之間。

我這樣做草圖顯示,也許更好,我需要, 感謝您的幫助!

$filter_year=2002;  

$posts = get_posts(array(
'posts_per_page' => -1, 
'post_type'  => 'post', 
'meta_query' => array(
    'relation'  => 'AND', 
    array(
     'key'  => 'start_year', 
     'value'  => $filter_year, 
     'compare' => '???', 
    ), 
    array(
     'key'  => 'end_year', 
     'value'  => $filter_year, 
     'compare' => '???', 
    ), 
), 
+0

是你試圖通過或自定義元字段篩選後的發佈日期?如果是的話,字段名稱是什麼? – David

+0

「START_YEAR」和「END_YEAR」是自定義元字段 – buckdanny

+0

大衛的名字,你有一個想法如何做到這一點?非常感謝! – buckdanny

回答

0

您可以對此使用元查詢。但是,如果這些工作尚未實施,則需要做出改變。

1:您可以使用元查詢的日期類型,但它只會在yyyy-mm-dd格式

$start_date = date('Y-m-d', strtotime('1/1/15')); // cant just use the year, it will see it as a time value 
$end_date = date('Y-m-d', strtotime('31/12/15')); // cant just use the year, it will see it as a time value 

'meta_query' => array(
    'relation' => 'AND' 
    array(
     'key' => 'start_year', 
     'value' => $start_date, 
     'compare' => '>=' 
    ), 
    array(
     'key' => 'start_year', 
     'value' => $end_date, 
     'compare' => '<=' 
    ), 
) 

2工作:轉換日期以時間戳(首選)

$start_date = strtotime('1/1/15'); // cant just use the year, it will see it as a time value 
$end_date = strtotime('31/12/15'); // cant just use the year, it will see it as a time value 

'meta_query' => array(
    'relation' => 'AND' 
    array(
     'key' => 'start_year', 
     'value' => $start_date, 
     'compare' => '>=' 
    ), 
    array(
     'key' => 'start_year', 
     'value' => $end_date, 
     'compare' => '<=' 
    ), 
) 

您可以循環在元值上使用wp_query或get_posts並檢查get_post_meta($postid, 'start_year', true);是否存在。