php - 根据 WordPress 中标题的一部分降序排序?

标签 php mysql wordpress

我在如何在 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/

相关文章:

php - Symfony - 无法使用安装程序创建新项目

php - Mysql 查询匹配月份和年份

php - tax_query 不适用于 WP_Query

php - WordPress wp查询LIKE语句

php - 如何在 SQL CONCAT 中转义 # 作为 URL 中的 anchor

php - 左侧的正则表达式不贪婪(即,两侧可能的最窄匹配)

php - 如何在 Zend Framework 2 中添加 Ajax 请求

PHP 和 MySQL 编码和奇怪的标志

php - 每行 Wordpress/WooCommerce 产品

html - 网站 iPad 显示呈现问题