Update post on save

时间:2016-11-21 作者:Sam Miller

我正在努力研究如何在帖子状态发生变化后更改其详细信息。

我想通过添加帖子id号来更改帖子的标题。下面是我现在正在使用的代码,但它不会更改帖子的标题:

function update_post_info( $post_id, $post, $update ) {

// Stop anything from happening if revision
if ( wp_is_post_revision( $post_id ) ) return;

//get post type
   $post_type = get_post_type($post_id);

// If this isn\'t a custom post, don\'t update it.
// if ( "cbre_access_form" != $post_type ) return;

     //run codes based on post status
      $post_status = get_post_status();
      if ( $post_status != \'draft\' ) 
      {
         if ( isset( $_POST[\'post_title\'] ) ) {
           //stuck on this part not changing post title
           $ppt = \'test title - \'.$post_id;
           update_post_meta( $post_id, \'post_title\', $ppt );
         }
      }
}
add_action( \'save_post\', \'update_post_info\', 10, 3 );

2 个回复
最合适的回答,由SO网友:Dave Romsey 整理而成

帖子标题(post_title) 未保存在元数据中;它是post表中的一个字段。

这是原始代码的更新版本。

add_action( \'save_post\', \'wpse246957_update_post_info\', 10, 3 );
function wpse246957_update_post_info( $post_id, $post, $update ) {

    // Stop anything from happening if revision
    if ( wp_is_post_revision( $post_id ) ) {
        return;
    }

    // unhook this function so it doesn\'t loop infinitely
    remove_action( \'save_post\', \'wpse246957_update_post_info\' );

    // get post type
    $post_type = get_post_type( $post_id );

    // If this isn\'t a custom post, don\'t update it.
    // if ( "cbre_access_form" != $post_type ) return;

    // run codes based on post status
    $post_status = get_post_status();
    if ( $post_status != \'draft\' )  {
        if ( isset( $_POST[\'post_title\'] ) ) {

            $suffix = \' - \' . $post_id;
            if ( ! preg_match( \'/\' . preg_quote( $suffix, \'/\' ) . \'$/\', $_POST[\'post_title\'] ) ) {
                wp_update_post( [
                        "ID"         => $post_id,
                        "post_title" => $_POST[\'post_title\'] . $suffix,
                ] );            
            }
        }
    }

    // re-hook this function
    add_action( \'save_post\', \'wpse246957_update_post_info\', 10, 3 );
}
就个人而言,我可能会在将标题输出到模板文件之前或在输出标题的任何地方添加后缀,因为在重新保存帖子标题时,上述方法可能存在一些边缘情况。

SO网友:Syed Fakhar Abbas

post_title 保存在posts 表格为post_title 不在post\\u meta中;此外,您还必须删除save_post 钩住然后添加,因为wp_update_post 导致激发save\\u post,它将创建infinite loop.


function update_post_info( $post_id, $post, $update ) {

// Stop anything from happening if revision
if ( wp_is_post_revision( $post_id ) ) return;

//get post type
   $post_type = get_post_type($post_id);

// If this isn\'t a custom post, don\'t update it.
// if ( "cbre_access_form" != $post_type ) return;

     //run codes based on post status
      $post_status = $post->post_status;
      if ( $post_status != \'draft\' ) 
      {
     if ( isset( $_POST[\'post_title\'] ) ) {
       $my_post = array(
        \'ID\'           => $post_id,
        \'post_title\'   => $_POST[\'post_title\'].\' - \'.$post_id,
       );
        if ( ! wp_is_post_revision( $post_id ) ) {

            // unhook this function so it doesn\'t loop infinitely
            remove_action(\'save_post\', \'update_post_info\');

            // update the post, which calls save_post again
            wp_update_post( $my_post );

            // re-hook this function
            add_action(\'save_post\', \'update_post_info\');
        }
       }
      }
}
add_action( \'save_post\', \'update_post_info\', 10, 3 );


相关推荐