我的帖子包含我希望按“LiveStreamDate”排序的元数据。元字段的格式为:yyyy / mm / dd。
我目前的代码如下:
$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
答案 0 :(得分: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'
(...)