When the user clicks on the preview I want to change the status of the post to "published" if not published and then reset my cache in my landing. Then after that, I want to revert the post status to the same stage. The last part of reverting to the same stage isn't working here is the code
add_filter( 'preview_post_link', function ( $link, \WP_Post $post )
{
$slug = $post->post_name;
$status_old = get_post_status($post->ID);
if($status_old != "publish"){
// return "/".$status_old;
$post = array('ID'=>$post->ID, 'post_status' => "publish" );
wp_update_post($post, true);
$response = wp_remote_request(";);
$post = array('ID'=>$post->ID, 'post_status' => $status_old );
// $post = array('ID'=>$post->ID, 'post_status' => "draft" );
wp_update_post($post, true);
} else {
$response = wp_remote_request(";);
}
return "mywebsite/".$slug;
}, 10, 2 );