Search code examples
wordpresshookposts

Update a post without update post title


I want to update post data without update post title even if user change the post title. For this functionality I have write the following codes. But it's not working. My code is as follows :

function update_post_without_update_title($post_id,$data) {
      $post = get_post($post_id);
      if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) 
         return;
      if ($data['post_status'] == "publish"){
          $data['post_title'] = $post->post_title;
       }
}

add_action('pre_post_update','update_post_without_update_title',10,2);

Can you tell me what to fix here ?

Thanks in advance,


Solution

  • Use this function :

     add_action('post_updated','after_update_post_without_update_title',10,3);
    
     function after_update_post_without_update_title($postId,$after,$before)
     {
            global $wpdb;
            $where = array( 'ID' => $postId );
            $oldTitle = $before->post_title;
            $data = array('post_title'=>$oldTitle);
            $wpdb->update( $wpdb->posts, $data, $where );
            return true;
     }