2011-08-20 59 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