$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>custom taxonomy - List Post Title by Last Word Then Second Last|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

custom taxonomy - List Post Title by Last Word Then Second Last

matteradmin10PV0评论

I have a CPT - Author where the title of the post is used as Author's name. I'm trying to list the authors alphabetically based on the last word (last name) and it works best.

Here is the code in the functions.php

add_filter( 'posts_orderby', function( $orderby, \WP_Query $q )
{
if( 'wpse_last_word' === $q->get( 'orderby' ) && $get_order =  $q->get( 'order' ) )
{
    if( in_array( strtoupper( $get_order ), ['ASC', 'DESC'] ) )
    {
        global $wpdb;
        $orderby = " SUBSTRING_INDEX( {$wpdb->posts}.post_title, ' ', -1 ) " . $get_order;
    }
}
return $orderby;
}, PHP_INT_MAX, 2 ); 

And to sort them, I use below code just before the query

add_filter( 'posts_orderby' , 'posts_orderby_lastname' );

My problem is when two Authors have the same last name. I want to still list them alphabetically on the second last word or the 3rd last word if the last/second last word is the same. E.g.

James Lucas
George Lucas

It currently lists the Authors based on the published date in case the last names are same.

How to list based on second last or third last word?

I have a CPT - Author where the title of the post is used as Author's name. I'm trying to list the authors alphabetically based on the last word (last name) and it works best.

Here is the code in the functions.php

add_filter( 'posts_orderby', function( $orderby, \WP_Query $q )
{
if( 'wpse_last_word' === $q->get( 'orderby' ) && $get_order =  $q->get( 'order' ) )
{
    if( in_array( strtoupper( $get_order ), ['ASC', 'DESC'] ) )
    {
        global $wpdb;
        $orderby = " SUBSTRING_INDEX( {$wpdb->posts}.post_title, ' ', -1 ) " . $get_order;
    }
}
return $orderby;
}, PHP_INT_MAX, 2 ); 

And to sort them, I use below code just before the query

add_filter( 'posts_orderby' , 'posts_orderby_lastname' );

My problem is when two Authors have the same last name. I want to still list them alphabetically on the second last word or the 3rd last word if the last/second last word is the same. E.g.

James Lucas
George Lucas

It currently lists the Authors based on the published date in case the last names are same.

How to list based on second last or third last word?

Share Improve this question asked Jan 21, 2019 at 12:49 Rakesh OjhaRakesh Ojha 237 bronze badges 4
  • this was answered here: wordpress.stackexchange/questions/198610/… – rudtek Commented Jan 21, 2019 at 15:52
  • The referenced answer doesn't seem to address my problem. I'm in fact using the same code and it works great as long as the last name is not the same. The above solution fails to sort when two or more authors have the same last name. So, how to sort authors on their first name when their last name is same? – Rakesh Ojha Commented Jan 21, 2019 at 16:55
  • did you try the 2nd answer? – rudtek Commented Jan 21, 2019 at 16:56
  • Yes. I copied the second answer's code and replaced that in the functions.php for add_filter, but no effect. This is how the listing is coming for my authors - Anthony Luckas - Rachel Lucas - Barbara Lucas. – Rakesh Ojha Commented Jan 21, 2019 at 17:02
Add a comment  | 

1 Answer 1

Reset to default 1

I think I got it!!!

Try this:

 $orderby = " SUBSTRING_INDEX( {$wpdb->posts}.post_title, ' ', -1 ) " . $get_order. " , {$wpdb->posts}.post_title " . $get_order;

Basically it's sorting by lastname and then first name in 1 query.

So, your whole code would be:

add_filter( 'posts_orderby', function( $orderby, \WP_Query $q )
{
if( 'wpse_last_word' === $q->get( 'orderby' ) && $get_order =  $q->get( 'order' ) )
{
    if( in_array( strtoupper( $get_order ), ['ASC', 'DESC'] ) )
    {
        global $wpdb;

       $orderby = " SUBSTRING_INDEX( {$wpdb->posts}.post_title, ' ', -1 ) " . $get_order. " , {$wpdb->posts}.post_title " . $get_order;
    }
}
return $orderby;
}, PHP_INT_MAX, 2 ); 
Post a comment

comment list (0)

  1. No comments so far