Actually I’d love to have an answer from @ialocin, because he suggested me the way. But as the code is a bit long for comment field I’m posting it here as an answer. But I’d love to accept an answer more appropriate to the situation:
After @ialocin’s suggestion, I got that, the $post_id
wasn’t accurate for a Preview Changes hit, so the $existing_locations
wasn’t getting the correct db info. Now I changed the following line:
$existing_locations = get_post_custom_values( "{$project_prefix}post_locations", $post_id );
into the following conditional lines:
if( $_POST[ 'wp-preview' ] === 'dopreview' ) {
//if it's a preview hit, grab the actual post's data
$actual_post_id = wp_get_post_parent_id( $post_id );
$existing_locations = get_post_custom_values( "{$project_prefix}post_locations", $actual_post_id );
} else {
//else, go with the actual post
$existing_locations = get_post_custom_values( "{$project_prefix}post_locations", $post_id );
}
Everything is working as expected in localhost. Though sometimes, the Preview hit fails the preview_id
and preview_nonce
etc. and doesn’t show the preview, but when they’re set, the preview is, exactly what it should be.