the wp_post_update isn’t working all the time

Question

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 "http://landing.hiver.space/".$status_old;
        $post = array('ID'=>$post->ID, 'post_status' => "publish" );
        wp_update_post($post, true);
        $response = wp_remote_request("https://mywebsite.com/reset");
        $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("https://mywebsite.com/reset");
    }
    return "mywebsite.com/".$slug;

 }, 10, 2 );
0
HIver Ninja 4 months 0 Answers 10 views 0

Leave an answer