2016-01-19 39 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);是否存在。