快速编辑时设置的自定义标题

时间:2017-01-02 作者:kida

我已经设置了一个没有标题支持的自定义帖子类型,并添加了一个代码,用于从自定义字段自动填充标题。在编辑帖子页面上创建或更新帖子时,这非常有效。问题是,当我通过快速编辑菜单更新帖子时,标题会返回到(无标题)。

有人知道我可以在代码中更改或添加什么内容,以便在使用快速编辑时保留标题吗?

这是我的代码:

        function kida_post_updated_func( $post_id ) {
    if ( wp_is_post_revision( $post_id ) || get_post_type($post_id) != \'plot_detail\')return;
remove_action( \'post_updated\', \'kida_post_updated_func\' );

$given = get_post_meta($id, \'wpcf-given\', true);
$givenslug = "given";
$nickname = get_post_meta($id, \'wpcf-nickname\', true);
$nicknameslug = "nickname";
$middle = get_post_meta($id, \'wpcf-middle\', true);
$middleslug ="middle";
$maiden = get_post_meta($id, \'wpcf-maiden-name\', true);
$maidenslug ="maiden-name";
$family = get_post_meta($id, \'wpcf-family\', true);
$familyslug = "family";

if(isset($_POST[\'wpcf\'][$givenslug]) && !empty($_POST[\'wpcf\'][$givenslug])){
    $a = $_POST[\'wpcf\'][$givenslug]; 
}
if(isset($_POST[\'wpcf\'][$nicknameslug]) && !empty($_POST[\'wpcf\'][$nicknameslug])){
    $b = $_POST[\'wpcf\'][$nicknameslug]; 
    $bb = \'"\';
}
if(isset($_POST[\'wpcf\'][$middleslug]) && !empty($_POST[\'wpcf\'][$middleslug])){
    $c = $_POST[\'wpcf\'][$middleslug]; 
}
if(isset($_POST[\'wpcf\'][$maidenslug]) && !empty($_POST[\'wpcf\'][$maidenslug])){
    $d = $_POST[\'wpcf\'][$maidenslug]; 
    $dd = \'(\';
    $de = \')\';
}
if(isset($_POST[\'wpcf\'][$familyslug]) && !empty($_POST[\'wpcf\'][$familyslug])){
  $e = $_POST[\'wpcf\'][$familyslug]; 
}


    $v = $a . \' \' . $bb . $b . $bb . \' \' . $c . \' \' .  $dd . $d . $de . \' \' . $e;


$my_args = array(
    \'ID\' => $post_id,
    \'post_title\' => $v,
    \'post_name\' => sanitize_title($v),
);

// update the post, which calls save_post again
$res = wp_update_post( $my_args, true );
add_action( \'post_updated\', \'kida_post_updated_func\' );
}
add_action( \'post_updated\', \'kida_post_updated_func\' );

1 个回复
SO网友:kida

感谢wordpress上的bcworkz。组织机构(https://wordpress.org/support/topic/custom-title-set-on-quick-edit/#post-8631035) 写作:

我不能百分之百确定,但我认为这是因为自定义字段数据没有通过快速编辑在$\\u POST中发送,因此标题最终被设置为空字段。我认为您需要做的是为与标题相关的每个自定义字段添加else代码,当缺少$\\u POST数据时,尝试从POST meta获取数据,如果不存在,请将标题片段设置为某个默认值。完全可选,但您可能会考虑使用“pre\\u post\\u update”操作而不是“post\\u updated”来更改标题。主要优点是,它可以保存额外的查询来保存帖子数据,因为您可以在保存之前更改数据,所以帖子不需要再次保存。>

因此,我为每个标题字段添加了else语句。如果有人试图实现同样的目标,下面是我的新工作代码:

function kida_post_updated_func( $post_id ) {
if ( wp_is_post_revision( $post_id ) || get_post_type($post_id) != \'plot_detail\')return;
 remove_action( \'post_updated\', \'kida_post_updated_func\' );
$given = get_post_meta($id, \'wpcf-given\', true);
    $givenslug = "given";
$nickname = get_post_meta($id, \'wpcf-nickname\', true);
    $nicknameslug = "nickname";
$middle = get_post_meta($id, \'wpcf-middle\', true);
$middleslug ="middle";
$maiden = get_post_meta($id, \'wpcf-maiden-name\', true);
$maidenslug ="maiden-name";
$family = get_post_meta($id, \'wpcf-family\', true);
$familyslug = "family";

if(isset($_POST[\'wpcf\'][$givenslug]) && !empty($_POST[\'wpcf\'][$givenslug])){
    $a = $_POST[\'wpcf\'][$givenslug]; 
}
else {
global $post;
    if( empty( $post ) )
    $post = get_post($post_id);
    if( get_post_type($post_id) !== \'plot_detail\' )
    return $post_id;
       if( function_exists(\'types_render_field\') ) {
         $a = types_render_field( \'given\', array(\'raw\'=>\'true\') );
   }
 }
if(isset($_POST[\'wpcf\'][$nicknameslug]) && !empty($_POST[\'wpcf\'][$nicknameslug])){
    $b = $_POST[\'wpcf\'][$nicknameslug]; 
    $bb = \'"\';
}
else {
   global $post;
   if( empty( $post ) )
   $post = get_post($post_id);
   if( get_post_type($post_id) !== \'plot_detail\' )
       return $post_id;
   if( function_exists(\'types_render_field\') ) {
    $bx = types_render_field( \'nickname\', array(\'raw\'=>\'true\') );
    if(!empty($bx)) {
       $b = $bx;
       $bb = \'"\';
    }
   }
}
if(isset($_POST[\'wpcf\'][$middleslug]) && !empty($_POST[\'wpcf\'][$middleslug])){
    $c = $_POST[\'wpcf\'][$middleslug]; 
}
else {
   global $post;
   if( empty( $post ) )
   $post = get_post($post_id);
   if( get_post_type($post_id) !== \'plot_detail\' )
    return $post_id;
   if( function_exists(\'types_render_field\') ) {
    $c = types_render_field( \'middle\', array(\'raw\'=>\'true\') );
   }
}       
if(isset($_POST[\'wpcf\'][$maidenslug]) && !empty($_POST[\'wpcf\'][$maidenslug])){
    $d = $_POST[\'wpcf\'][$maidenslug]; 
    $dd = \'(\';
    $de = \')\';
}
else {
   global $post;
   if( empty( $post ) )
   $post = get_post($post_id);
   if( get_post_type($post_id) !== \'plot_detail\' )
   return $post_id;
   if( function_exists(\'types_render_field\') ) {
    $dx = types_render_field( \'maiden-name\', array(\'raw\'=>\'true\') );
    if(!empty($dx)) {
       $d = $dx;
       $dd = \'(\';
       $de = \')\';
    }
    }
}
if(isset($_POST[\'wpcf\'][$familyslug]) && !empty($_POST[\'wpcf\'][$familyslug])){
    $e = $_POST[\'wpcf\'][$familyslug]; 
}
else {
   global $post;
   if( empty( $post ) )
   $post = get_post($post_id);
   if( get_post_type($post_id) !== \'plot_detail\' )
    return $post_id;
   if( function_exists(\'types_render_field\') ) {
    $e = types_render_field( \'family\', array(\'raw\'=>\'true\') );
   }
}

$v = $a . \' \' . $bb . $b . $bb . \' \' . $c . \' \' .  $dd . $d . $de . \' \' . $e;


$my_args = array(
    \'ID\' => $post_id,
    \'post_title\' => $v,
    \'post_name\' => sanitize_title($v),
);

// update the post, which calls save_post again
$res = wp_update_post( $my_args, true );
add_action( \'post_updated\', \'kida_post_updated_func\' );
}
add_action( \'post_updated\', \'kida_post_updated_func\' );`

相关推荐

删除具有实际标题的SANITIZE_TITLE_WITH_DASSES函数

求你了,我真的需要帮助。下面的代码是在我的网站上将搜索查询保存为标记的代码,但它将标题替换为“-”,请问有什么方法可以使标记标题名称保持搜索状态?。function addsometags() { //Don\'t do anything if we\'ve already got 20 tags $posttags = get_the_tags(); $count=0; if ($posttags) { foreach($posttags as $tag