我正在尝试使用Wordpress的下一个/上一个帖子链接功能,在基于姓氏的自定义类型的帖子之间移动。
自定义类型表示具有“名字姓氏”格式的职位头衔的人。
我已经按姓氏在档案页面上订购了这些帖子,但我正在努力找出如何让下一个/上一个链接也遵循此过滤器。我看到另一个similar question 并已成功按标题排序下一个/上一个链接,但仅按名字的字母顺序排序。
如果有人知道如何做到这一点,将不胜感激。
我正在尝试使用Wordpress的下一个/上一个帖子链接功能,在基于姓氏的自定义类型的帖子之间移动。
自定义类型表示具有“名字姓氏”格式的职位头衔的人。
我已经按姓氏在档案页面上订购了这些帖子,但我正在努力找出如何让下一个/上一个链接也遵循此过滤器。我看到另一个similar question 并已成功按标题排序下一个/上一个链接,但仅按名字的字母顺序排序。
如果有人知道如何做到这一点,将不胜感激。
查看MySQL string functions, 看起来你可以用SUBSTRING_INDEX()
功能:
ORDER BY SUBSTRING_INDEX( p.post_title, \' \', -1 ) {ASC|DESC} LIMIT 1
按文章标题的最后一个字排序。您可以在orderby过滤器中测试此方法。
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\'
满足您的需求。我想搜索custom_post 按标题和ACF字段。所以,我用了WP_Query WordPress函数,但我不能按标题过滤,只能按过滤器过滤。当我提交表单时,我有这样的URL:http://example.com/?s=titre&filter1=condition1&filter2=condition2&filter3=condition3 我的代码:$title = $_GET[\'s\']; $args = array( \'pagenam