这可能吗?如何更改相邻帖子的查询并添加"post_status=future"以显示预定帖子的下一个和上一个帖子?
我想我应该使用某种过滤器,但有人可以告诉我怎么做吗?:)
previous_post_link
使用adjacent_post_link
,而又使用包含名为get_{$adjacent}_post_where
的SQL查询字符串过滤器的get_adjacent_post
。使用此过滤器,$adjacent
交换为previous
或next
,您可以通过以下方式使链接生成器搜索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);
}