Tag Archives: 自定义栏目排序

wordpress通过自定义栏目排序,不添加,也可以正常显示

在主题的functions.php中添加以下代码
_li_love_count为自定义的字段名称

function lovesort($query)
{ $query->set('meta_query', array(
'relation' => 'OR',
array( //check to see if _li_love_count has been filled out
'key' => '_li_love_count',
'compare' => '=',
'value' => int
),
array( //if no _li_love_count has been added show these posts too
'key' => '_li_love_count',
'value' => int,
'compare' => 'NOT EXISTS'
)
) );
$query->set('meta_key' , '_li_love_count');
$query->set('orderby' , 'meta_value_num');
$query->set('order' , 'desc');
}
add_filter('pre_get_posts' , 'lovesort');