查看MySQL string functions, 看起来你可以用SUBSTRING_INDEX()
功能:
ORDER BY SUBSTRING_INDEX( p.post_title, \' \', -1 ) {ASC|DESC} LIMIT 1
按文章标题的最后一个字排序。
您可以在orderby过滤器中测试此方法。
示例:上一个/下一个CPT-按文章标题中的最后一个单词排序这是一个如何尝试修改链接的示例answer 作者@WillLanni:
a) 自定义帖子类型的下一部分cpt
:
// Next CPT
add_filter( \'get_next_post_where\', function( $where, $in_same_term, $excluded_terms )
{
global $post, $wpdb;
// Edit this custom post type to your needs
$cpt = \'post\';
// Current post type
$post_type = get_post_type( $post );
// Nothing to do
if( $in_same_term || ! empty( $excluded_terms ) || $cpt !== $post_type )
return $where;
// Next CPT order by last word in title
add_filter( \'get_next_post_sort\', function( $orderby )
{
return " ORDER BY SUBSTRING_INDEX( p.post_title, \' \', -1 ) ASC LIMIT 1 ";
} );
// Modify Next WHERE part
return $wpdb->prepare(
" WHERE
SUBSTRING_INDEX( p.post_title, \' \', -1 ) > SUBSTRING_INDEX( \'%s\', \' \', -1 )
AND p.post_type = \'%s\'
AND p.post_status = \'publish\'
",
$post->post_title,
$post_type
);
}, 10, 3 );
b) 自定义帖子类型的上一部分
cpt
:
// Previous CPT
add_filter( \'get_previous_post_where\', function( $where, $in_same_term, $excluded_terms)
{
global $post, $wpdb;
// Edit this custom post type to your needs
$cpt = \'post\';
// Current post type
$post_type = get_post_type( $post );
// Nothing to do
if( $in_same_term || ! empty( $excluded_terms ) || $cpt !== $post_type )
return $where;
// Previous CPT, order by last word in post title
add_filter( \'get_previous_post_sort\', function( $orderby )
{
return " ORDER BY SUBSTRING_INDEX( p.post_title, \' \', -1 ) DESC LIMIT 1 ";
} );
// Modify Prev WHERE part
return $wpdb->prepare(
" WHERE
SUBSTRING_INDEX( p.post_title, \' \', -1 ) < SUBSTRING_INDEX( \'%s\', \' \', -1 )
AND p.post_type = \'%s\'
AND p.post_status = \'publish\'
",
$post->post_title,
$post_type
);
}, 10, 3 );
在其中修改自定义帖子类型
\'cpt\'
满足您的需求。