Home > Back-end >  Wp update post not updating
Wp update post not updating

Time:11-02

I'm trying to update a post title using wp_update_post()

First I get the ID of the post:

global $post;
$post_to_edit = get_post($_GET['post_id']);

Then the form to update which it actually gets the title from the post to edit:

<form action="" id="primaryPostForm" method="POST">
   <label for="postTitle">Title of your post</label>
   <input type="text" name="postTitle" id="postTitle"  value="<?php echo esc_html( $post_to_edit->post_title ); ?> ">
   <input type="hidden" name="postId" id="postId" value="<?php echo $_GET['post_id']; ?>">   
   <input type="submit" name="submit_post" value="Update">       
</form>

Then below on the same page I have:

<?php
    if ($_SERVER['REQUEST_METHOD'] === 'POST') {
        if (isset($_POST['submit_post'])) {
            $data = array(
                'ID' => $_POST['postId'],
                'post_title' => $_POST('postTitle')
            );
            wp_update_post( $data );
        }
    }
?>

But it's not updating the post title.

CodePudding user response:

Your code is wrong,

<?php
    if ($_SERVER['REQUEST_METHOD'] === 'POST') {
        if (isset($_POST['submit_post'])) {
            $data = array(
                'ID' => $_POST['postId'],
                'post_title' => $_POST['postTitle']    //you used wrong brackets
            );
            wp_update_post( $data );
        }
    }
?>

  • Related