I never found the real reason for this issue. However this “bugfix” did solve the issue for me:
add_action( 'template_redirect', 'handle_strange_bug', 10 );
// "Fixes" the strange bug...
function handle_strange_bug() {
global $post;
$post_id = 123; // This is the post that makes problems.
/* ===== Start of the bugfix.
*
* If the $post->ID is not set, but the URL indicates we are on the problem-
* page then do a redirect to an alternative URL for the page. After the
* redirect we always had a valid $post->ID, so
*/
if ( !isset($post->ID) ) {
// The function is defined below.
$my_url = get_current_url();
if ( $my_url == get_permalink($post_id) ) {
$good_url = site_url() . '?page_id=' . $post_id;
wp_redirect($good_url);
}
}
// ===== End of the bugfix.
}
// Helper function. Returns the full URL of the current request.
function get_current_url() {
if ( @$_SERVER['HTTPS'] == 'on' ) {
$page_url="https://";
} else {
$page_url="http://";
}
$page_url .= @$_SERVER['SERVER_NAME'];
if ( @$_SERVER['SERVER_PORT'] != '80' ) {
$page_url .= ':' . @$_SERVER['SERVER_PORT'];
}
$page_url .= @$_SERVER['REQUEST_URI'];
// Remove the Query-string from the URL.
if ( $pos_get = strpos($page_url, '?') ) {
$page_url = substr($page_url, 0, $pos_get);
}
return $page_url;
}