我正在尝试按字母顺序对WP中的帖子进行排序,但我没有得到我所期望的结果。这是我得到的:
NOZ - 1 apple
NOZ - 10 orange
NOZ - 11 banana
NOZ - 2 tree
NOZ - 3 grass 这是我想要得到的:
NOZ - 1 apple
NOZ - 2 tree
NOZ - 3 grass
NOZ - 10 orange
NOZ - 11 banana这是我的PHP代码:
<?php $loop = new WP_Query( array('post_type' => 'myPosts', 'orderby' => 'title', 'order' => 'ASC', 'posts_per_page' => -1 ) ); ?>非常感谢你的帮助。
发布于 2017-07-01 13:50:50
您可以将MySQL函数添加到orderby字段。例如:
'orderby' => 'CAST(SUBSTRING(title, 7) as unsigned)'substring函数提取第7位字符之后的所有字符并包括该字符。
cast函数将提取的字符转换为无符号整数。cast函数从左到右工作。它将丢弃所有不是数字的字符。这里回答了一个类似的问题:SQL order string as number
https://stackoverflow.com/questions/44829035
复制相似问题