2017-06-29 67 views
1

我想按字母順序排列WP中的帖子,但是我沒有得到我期待的結果。這就是我得到:Wordpress - 按字母順序排列包含數字和字母的帖子

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) ); ?> 

非常感謝你的幫助。

回答

2

您可以將MySQL函數添加到orderby字段。例如:

'orderby' => 'CAST(SUBSTRING(title, 7) as unsigned)' 

子字符串函數提取第7個位置後面包括該字符的所有字符。

轉換函數將提取的字符轉換爲無符號整數。演員功能從左到右工作。它放棄所有不是數字的字符。在這裏回答了類似的問題:SQL order string as number

+0

太棒了,它的工作,非常感謝! –