我在如何在 Wordpress 中对标题进行排序时遇到问题。如果我知道如何在 MySQL 中解决这个问题那就太好了。 (如果有人有专门针对 WordPress 的答案,那对我来说会容易得多)
我有一个数据库,其中标题(数据库表中的一个字段)称为 post_title。标题存储名称和年份,中间有一个空格
例如。
Sweden 2017
Sweden 2016
Norway 2018
Norway 2019
我想像这样对上面进行排序:(按年份降序,按名称升序)
Norway 2019
Norway 2018
Sweden 2017
Sweden 2016
更新: 我尝试过使用 WP_Query 进行 Wordpress 内置排序:
$module_args = array(
'posts_per_page' => -1,
'post_type' => 'project',
'include_children' => false,
'orderby' => 'title',
'order' => 'DESC',
'no_found_rows' => true //No pagination needed
);
//Do the filtering and retrieve all projects with current service
$projects = get_posts( $module_args );
但是我得到了这个结果:
Yacht 2011
Vasastan 2008
Ulrikagatan 2008
Thailand 2010 (?)
Strandvägen 2007
但应该是:
Yacht 2011
Thailand 2010
Ulrikagatan 2008
Vasastan 2008
Strandvägen 2007
最佳答案
请使用以下代码来获取您所需要的内容:
$args = array(
'orderby' => 'title',
'order' => 'DESC',
);
$query = new WP_Query( $args );
关于php - 根据 WordPress 中标题的一部分降序排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43139582/