更新没有更新帖子标题的帖子

时间:2015-06-02 11:20:25

标签: wordpress wordpress-plugin hook posts

即使用户更改帖子标题,我也希望更新帖子数据而不更新帖子标题。 对于此功能,我编写了以下代码。但它不起作用。 我的代码如下:

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);

你能告诉我这里要解决什么问题吗?

提前致谢,

1 个答案:

答案 0 :(得分:1)

使用此功能:

 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;
 }
相关问题