Problem
There is a pice of code that makes scheduled posts available for outside users via URL. The code itself works just fine - however it seems to mess with the preview feature for the editor. What causes this problem and how can it be fixed?
The code
The code below is what I use for making schedulesd Posts available:
//this is inside the functions.php
function show_future_posts($posts)
{
global $wp_query, $wpdb;
if (is_single() && $wp_query->post_count == 0) {
$posts = $wpdb->get_results($wp_query->request);
for ($i = 0; $i < sizeof($posts); $i++) {
if ($posts[$i]->post_status == 'trash') {
unset($posts[$i]);
$posts = array_values($posts);
}
}
}
return $posts;
}
add_filter('the_posts', 'show_future_posts');
Steps to reproduce
- create new post
- schedule post
- edit somthing
- hit preview
Some observations:
- as long as the post is marked as draft, the preview works fine.
- after updating/saving (ctrl + s) the preview is correct
- I dont think this is related to already known issues like this. When removing the code in question the autosave-feature works just fine
- published posts show the same behaviour as scheduled ones
- the editor (gutenberg/default) doesn`t seem to matter in this case
any help is much appreciated. Thanks!