【问题标题】:WordPress custom order filter by last name in post titleWordPress自定义订单过滤器按帖子标题中的姓氏
【发布时间】:2021-02-10 02:25:02
【问题描述】:

我有一个自定义的员工帖子类型,它使用帖子标题作为人名。为了按姓氏排序,我使用顺序过滤器来查找标题中的最后一个单词,然后按它排序:

function posts_orderby_lastname ($orderby_statement) 
{
  $orderby_statement = "RIGHT(post_title, LOCATE(' ', REVERSE(post_title)) - 1) ASC";
    return $orderby_statement;
}

这对于大多数名字和姓氏都正常的员工来说非常有用,但我不知道如何对像这样的名字执行此操作(所有这些都应按“条款”排序:

圣诞老人

圣诞老人 III

小圣诞老人

圣诞老人克林格尔

M.圣诞老人老

我假设我可以有一个存储数组,然后检查这些术语(如“Jr.”、“II”等)或检查找到的术语的长度是否大于 3,但我没有想法如何在代码中实现它。任何想法或帮助将不胜感激 - 在此先感谢!

【问题讨论】:

    标签: mysql wordpress filter sql-order-by


    【解决方案1】:

    我遇到了类似的问题,我就是这样解决的。您可以针对您描述的所有边缘情况扩展 if 语句。

    $args = array(
        'post_type'        => 'page',
        'orderby'          => 'menu_order',
        'posts_per_page'   => -1,
        'post_parent'      => $post->ID,
        'order'            => 'ASC'
    );
    $posts = get_posts( $args );
    
    // Order by second word in title, deal with edge cases
    $lastname = array();
    foreach( $posts as $key => $post ) {
        $word = explode( ' ', $post->post_title );
        $name = null;
    
        if( strlen($word[1]) == 1 ) {
            // Second word only 1 letter, so use third word if set
            $name = $word[2];
    
        } elseif( $word[3] == 'Sr.' ) {
            // Third word is 'Sr.', so use 2nd word
            $name = $word[1];               
    
        } else {
            $name = $word[1];
        }
    
        $lastname[$key] = $name;
    }
    array_multisort( $lastname, SORT_ASC, $posts ); 
    
    // Loop through posts
    foreach( $posts as $post ) {
       echo $post->post_title;
    }
    

    【讨论】:

      【解决方案2】:

      我稍作修改以满足我的需要,因为我的名字就像

      黎明 A. 阿德尔森

      蒂莫西·史密斯

      小约翰·A·史密斯

              $args = array(
                  'post_type'        => 'YOUR_POST_TYPE',
                  'posts_per_page'   => -1,
                  'order'            => 'ASC'
              );
              $posts = get_posts( $args );
      
              // Order by second word in title, deal with edge cases
              $lastname = array();
              foreach( $posts as $key => $post ) {
                  $word = explode( ' ', $post->post_title );
                  $name = null;
      
                  if( strlen($word[1]) == 2 ) {
                      // Second word 1 letter and dot, so use third word if set
                      $name = $word[2];
      
                  } elseif( $word[3] == 'Jr.' ) {
                      // Third word is 'Jr.', so use 2nd word
                      $name = $word[2];               
      
                  } else {
                      $name = $word[1];
                  }
      
                  $lastname[$key] = $name;
              }
              array_multisort( $lastname, SORT_ASC, $posts ); 
      
              // Loop through posts
              foreach( $posts as $post ) {
                  ?>
                  <div>
                      <?php  echo $post->post_title; ?>
                  </div>
                <?php
              }
      
              ?>
          </div>
      

      【讨论】:

        【解决方案3】:

        这是一种更好的方法,至少其他方法对我根本不起作用。 这将删除第一个空格之前的任何内容,并对字符串的其余部分应用标准排序。请务必根据您的实际需要修改is_main_query()is_category()

        function posts_orderby_lastname( $orderby_statement )
        {
            if( is_main_query() && is_category() ) {
                return "SUBSTRING_INDEX(post_title, ' ', -1)";
            } else {
                return $orderby_statement;
            }
        }
        

        【讨论】:

          【解决方案4】:

          【讨论】:

          • 感谢您的回复。抱歉 - 我正在尝试在没有插件或拖放的情况下执行此操作。我的客户应该能够添加任何名称的员工,并且前端应该按姓氏正确排序。除了上面列出的奇怪的名称实例之外,我现在拥有的功能*几乎可以准确地完成。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2019-02-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多