Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Wp update post not updating

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<form action="" id="primaryPostForm" method="POST">
   <label for="postTitle">Title of your post</label>
   <input type="text" name="postTitle" id="postTitle" class="form-control" 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.

>Solution :

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 );
        }
    }
?>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading