Set WordPress post status to ‘Draft’ from front end similar to get_delete_post_link

I am using the below code to allow a logged in user to set delete their own posts from the front end. Is there a way to do the same thing but setting the post to ‘draft’ rather than deleting it completely?

<?php if ($post->post_author == $current_user->ID) { ?>
   <p><a onclick="return confirm('Are you SURE you want to delete this?')" href="<?php echo get_delete_post_link( $post->ID ) ?>">Delete post</a></p>
<?php } ?>

Related posts

1 comment

  1. use this function wp_update_post(), you can test with this example:

    First create a form were you want to users select if the post are going to be a draft

    <form action="" method="POST" >
        <input type="checkbox" value="ok" name="draft">
        <input type="submit" value="Ok">
    </form>
    

    Then create a function to save the new state put this in function.php:

    function toDraft($pid){
    
    $toDraft = $_POST['draft'];
       if($toDraft == 'ok'){
          echo "string";
          wp_update_post(array('ID' => $pid, 'post_status'   =>  'draft'));
      }
    
    }
    

    Then add this function below the form you create.

    toDraft($post->ID);
    

    And test. Read this to know more about update post status

Comments are closed.