2011-08-20 56 views
0

我有我想按'LiveStreamDate'排序的元數據的帖子。元字段的格式爲:yyyy/mm/dd。使用WP_Query按日期排序WordPress Meta字段

我當前的代碼如下:

$recent = new WP_Query('cat='.$spcatid.'&paged=' . $paged); while($recent->have_posts()) : $recent->the_post(); 
$tmpLiveTime = get_post_custom_values("LiveStreamTime"); 
$tmpLiveDate = get_post_custom_values("LiveStreamDate"); 
$tmpLiveCompetition = get_post_custom_values("LiveStreamCompetition"); 
$tmpLiveMatch = get_post_custom_values("LiveStreamMatch"); 

正常回路的東西在這裏

任何想法?我已經使用元字段和值來查看WP_QUERY示例 - 但是如何構造查詢(或添加另一個查詢)以最終按元數據按日期值排序數據?

乾杯 BK

回答

2

可以使用遵循例如:

add_filter('posts_orderby', 'my_filter_posts_orderby'); 
$query = new WP_Query(array(
    'meta_key' => 'LiveStreamDate', 
    'cat' => $spcatid, 
    'paged' => $paged, 
)); 
remove_filter('posts_orderby', 'my_filter_posts_orderby'); 

function my_filter_posts_orderby($orderby) 
{ 
    global $wpdb; 
    $orderby = $wpdb->postmeta . '.meta_value DESC, ' . $orderby; 
    return $orderby; 
} 

while($query->have_posts()) { 
$query->the_post(); 
echo get_post_custom_values("LiveStreamDate"); 
} 

可能的參數列表在WordPress的文件

/wp-includes/query.php

這個函數裏面:

/** 
    * Fills in the query variables, which do not exist within the parameter. 
    * 
    * @since 2.1.0 
    * @access public 
    * 
    * @param array $array Defined query variables. 
    * @return array Complete query variables with undefined ones filled in empty. 
    */ 
    function fill_query_vars($array) { 
     $keys = array(
      'error' 
      , 'm' 
      , 'p' 
      , 'post_parent' 
      , 'subpost' 
      , 'subpost_id' 
      , 'attachment' 
      , 'attachment_id' 
      , 'name' 
      , 'static' 

(...)

+0

爽 - 如何在類ID添加? – bryan

+0

您可以添加過濾器像這樣的分類名稱: '$查詢=新WP_Query(陣列( 'meta_key'=> 'LiveStreamDate', 'CATEGORY_NAME'=> '未', ));' 或由ID等: '$查詢=新WP_Query(陣列( 'meta_key'=> 'LiveStreamDate', '貓'=> '138', ));' 我更新的答覆。 – leticia

+0

嗨工作一種享受!謝謝...現在唯一的問題是,它顯示重複的帖子標題...任何快速殺死這些的方法? – bryan