在 WordPress post 导航(previous_post_link、next_post_link)中包含计划(未来)posts
Include scheduled ( future ) posts in WordPress post navigation ( previous_post_link, next_post_link )
这可能吗?我如何更改相邻帖子的查询并添加 'post_status=future' 以显示预定帖子的下一个和上一个帖子?
我想我应该使用某种过滤器,但有人可以告诉我怎么做吗? :)
previous_post_link
使用 adjacent_post_link
,后者又使用 get_adjacent_post
,后者包含一个名为 get_{$adjacent}_post_where
的 SQL 查询字符串过滤器。使用此过滤器,将 $adjacent
交换为 previous
或 next
,您可以使 link 生成器按以下方式搜索 status = publish
以外的帖子实例:
add_filter( 'get_previous_post_where', function( $query_string ) {
$new_status_query_string_repl = "(p.post_status = 'publish' OR p.post_status = 'future')";
$new_query = preg_replace( "/p\.post_status = 'publish'/", $new_status_query_string_repl, $query_string );
return $new_query;
});
注意:没有对此进行测试,可能需要调整。但是钩子在那里,应该更改查询字符串以在相邻的帖子查询中包含 future
个帖子。
感谢 David Gard,找到了完美的解决方案。
add_filter('get_previous_post_where', 'my_add_future_posts_to_nav', 3, 99);
add_filter('get_next_post_where', 'my_add_future_posts_to_nav', 3, 99);
function my_add_future_posts_to_nav($where, $in_same_term, $excluded_terms){
return str_replace("p.post_status = 'publish'", "p.post_status IN ('publish', 'future')", $where);
}
这可能吗?我如何更改相邻帖子的查询并添加 'post_status=future' 以显示预定帖子的下一个和上一个帖子?
我想我应该使用某种过滤器,但有人可以告诉我怎么做吗? :)
previous_post_link
使用 adjacent_post_link
,后者又使用 get_adjacent_post
,后者包含一个名为 get_{$adjacent}_post_where
的 SQL 查询字符串过滤器。使用此过滤器,将 $adjacent
交换为 previous
或 next
,您可以使 link 生成器按以下方式搜索 status = publish
以外的帖子实例:
add_filter( 'get_previous_post_where', function( $query_string ) {
$new_status_query_string_repl = "(p.post_status = 'publish' OR p.post_status = 'future')";
$new_query = preg_replace( "/p\.post_status = 'publish'/", $new_status_query_string_repl, $query_string );
return $new_query;
});
注意:没有对此进行测试,可能需要调整。但是钩子在那里,应该更改查询字符串以在相邻的帖子查询中包含 future
个帖子。
感谢 David Gard,找到了完美的解决方案。
add_filter('get_previous_post_where', 'my_add_future_posts_to_nav', 3, 99);
add_filter('get_next_post_where', 'my_add_future_posts_to_nav', 3, 99);
function my_add_future_posts_to_nav($where, $in_same_term, $excluded_terms){
return str_replace("p.post_status = 'publish'", "p.post_status IN ('publish', 'future')", $where);
}