订单发布集数(Wordpress)
问题描述:
我的一些帖子出现故障(http://imgur.com/a/teVA3)所以我试图在主题函数中添加这个代码,让它按顺序排序,没有运气。订单发布集数(Wordpress)
add_action('pre_get_posts', 'my_change_sort_order');
function my_change_sort_order($query)
{
if(is_archive()):
//If you wanted it for the archive of a custom post type use: is_post_type_archive($post_type)
//Set the order ASC or DESC
$query->set('order', 'DESC');
//Set the orderby
$query->set('orderby', 'SUBSTRING_INDEX(post_title, "Episode ", 1) ASC, CAST(SUBSTRING_INDEX(post_title, "Episode ", -1) AS SIGNED)');
endif;
};
任何帮助将不胜感激。
答
你可以试试这个
add_action('pre_get_posts', 'my_change_sort_order');
function my_change_sort_order($query)
{
if(is_archive()):
$query->set('orderby', array('date' => 'DESC', 'title'=> 'DESC'));
endif;
};
如果我是你,我会做的集数了一块元数据,然后排序依据的元价值。 – Aibrean
如果您不能像Aibrean建议的那样将该集添加为元数据,您可以在获取它们后自行对帖子进行排序 - 而不是在循环中显示它们,将它们作为数组并在那里进行排序。 – FluffyKitten