1

I have set up a custom post type without title support and have added a code for the title to be automatically populated from custom fields. This works great when creating or updating a post on the edit post page. The problem is, when I update the post via the quick edit menu, the title goes back to (no title).

Would anyone know what I could change or add in my code so that the title remains when using quick edit?

Here is my code:

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

Thanks to bcworkz on wordpress.org (https://wordpress.org/support/topic/custom-title-set-on-quick-edit/#post-8631035) writing :

I can’t be 100% sure, but I think it’s because the custom field data is not sent in $_POST with quick edit, so the title ends up being set as an empty field. I think what you need to do is add else code for each custom field involved with the title where when the $_POST data is missing, attempt to get the data from post meta, and if that does not exist, set the title fragment to some default value. Totally optional, but you might look into using the “pre_post_update” action instead of “post_updated” to alter the title. The main advantage is it saves extra queries to save post data because you alter data before it is saved so the post does not need to be saved again.>

So I added the else statement for each title field. If anyone is trying to achieve the same thing, here is my new working code:

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' );`
  • Hi - you can help keep WPSO tidy by accepting your own answer! – Andy Macaulay-Brook Mar 19 '18 at 16:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.