我会使用wp\\u查询。
将此放在您的函数中。php:
/**
* Order posts by the last word in the post_title.
* Activated when orderby is \'wpse_last_word\'
*/
add_filter(\'posts_orderby\', function($orderby_sql, \\WP_Query $q) {
$orderbys = $q->get(\'orderby\');
if (!$orderbys) {
return;
}
if ($orderby_sql) {
$orderby_sql_array = [$orderby_sql];
}
else {
$orderby_sql_array = [];
}
if (!is_array($orderbys)) {
$words = explode(\' \', $orderbys);
$orderbys = [];
foreach ($words as $word) {
$orderbys[$word] = $q->get(\'order\');
}
}
global $wpdb;
foreach ($orderbys as $orderby => $direction) {
if ($orderby == \'wpse_last_word\') {
if (!$direction || !in_array(strtoupper($direction), [\'ASC\', \'DESC\'])) {
$direction = \'DESC\';
}
$orderby_sql_array[] = "SUBSTRING_INDEX({$wpdb->posts}.post_title, \' \', -1) $direction";
}
}
return implode(\', \', $orderby_sql_array);
}, 100, 2);
现在,您可以在类别3中执行此操作。php(未测试):
<?php query_posts($query_string . \'&orderby=wpse_last_word&order=ASC\');?>
<?php
if ( have_posts() ) : while ( have_posts() ) : the_post();
get_template_part( \'content-clients\', get_post_format() );
endwhile;
endif; ?>
但您肯定可以使用wp\\u查询并将其用作参数:(如果需要,添加其他参数)
$args = [
\'posts_per_page\' => 10,
\'post_type\' => \'post\',
\'orderby\' => \'wpse_last_word\',
\'order\' => \'ASC\'
];
$the_query = new WP_Query( $args );