【问题标题】:Wordpress - sort posts containing numbers and letters alphabeticallyWordpress - 按字母顺序对包含数字和字母的帖子进行排序
【发布时间】:2017-06-29 15:15:16
【问题描述】:

我正在尝试按字母顺序对 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 )    ); ?>

非常感谢您的帮助。

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    您可以将 MySQL 函数添加到 orderby 字段。例如:

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

    substring 函数提取第 7 位字符之后(包括第 7 位字符)的所有字符。

    强制转换函数将提取的字符转换为无符号整数。 cast 函数从左到右工作。它丢弃所有非数字字符。这里回答了一个类似的问题:SQL order string as number

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-02
      • 1970-01-01
      • 2013-08-02
      • 1970-01-01
      相关资源
      最近更新 更多