将自定义字段值复制到帖子标题

时间:2016-06-28 作者:Alex M

我使用了找到的代码here. 但是,在打开帖子并通过后端和前端点击更新按钮之前,这不会更新标题字段。我尝试用“edit\\u post”、“publish\\u Artister”和“wp\\u insert\\u post\\u data”更改“save post”挂钩,但仍然得到相同的结果。下面是我在使用“wp\\u insert\\u post\\u data”挂钩时使用的代码。

add_filter( \'wp_insert_post_data\' , \'modify_post_title\' , \'99\', 2 ); // Grabs the inserted post data so you can modify it.

function modify_post_title( $data )
{
  if($data[\'post_type\'] == \'artist\' && isset($_POST[\'artist_name\'])) { // The custom field
    $ArtistName = $_POST[\'artist_name\'];
    $data[\'post_title\'] =  $ArtistName ; //Updates the post title to your new title.
  }
  return $data; // Returns the modified data.
}

1 个回复
最合适的回答,由SO网友:Alex M 整理而成

可以最后,我终于成功了。下面是完整的工作代码。

add_filter( \'wp_insert_post_data\' , \'modify_post_title\' , \'99\', 2 ); // Grabs the inserted post data so you can modify it.

function modify_post_title( $data )
{
  if($data[\'post_type\'] == \'artist\' && isset($_POST[\'acf\'][\'field_573676a95d920\'])) { // If the actual field name of the rating date is different, you\'ll have to update this.
    $ArtistName = $_POST[\'acf\'][\'field_573676a95d920\'];
    $data[\'post_title\'] =  $ArtistName ; //Updates the post title to your new title.
  }
  return $data; // Returns the modified data.
}

//Save ACF field as post_title for front-end
add_action(\'acf/save_post\', \'change_artist_title_frontend\');

function change_artist_title_frontend($post_id) {
    global $_POST;
    if(\'artist\'== get_post_type())
    {
        $post_custom_title = $_POST[\'acf\'][\'field_573676a95d920\'];
        $my_post = array();
                $my_post[\'ID\'] = $post_id;
                $my_post[\'post_title\'] = $post_custom_title;
remove_action(\'acf/save_post\', \'change_artist_title_frontend\');
                    wp_update_post( $my_post );
add_action(\'acf/save_post\', \'change_artist_title_frontend\');
    } 
}

相关推荐

如何在Functions.php中链接style.css

我是WordPress的新手;我刚开始学习WordPress。我想把风格联系起来。函数中的css。php,但我无法解决这里可能存在的问题。谁能给我指出正确的方向吗?指数php<?php get_header(); ?> <?php if ( have_posts() ) { while ( have_posts() ) { the_post();