• Hello,
    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' );
Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    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.

    Thread Starter kida18

    (@kida18)

    Perfect!! If I set the post meta as else for each title field, it works ??

    Here is my code in case someone looks to achieve the same thing:

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

    Regarding the “pre_post_update” – If I use that instead then my title doesn’t get set and shows as “auto-draft” whether it’s a new created post or an update.

    Thanks a lot for your help @bcworkz!

    Moderator bcworkz

    (@bcworkz)

    You’re welcome!

    Since you have working code, you’re probably not interested in any more alternates. Quite understandable. In any case, for the sake of correcting my own error, I gave you the wrong hook with “pre_post_update”. That’s an action that doesn’t do what we need, as you discovered. Sorry about the bad information.

    We want the filter “wp_insert_post_data”, the next hook up from pre_post_update in source. Changing the title in the passed array will do the trick. (Use addslashes() on any introduced data) No need to worry about removing hooks to prevent infinite loops and no re-saving the same post data.

    For anyone landing here from a search, this is the way to go if you’re able to work up the code to do so.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Custom title set on quick edit’ is closed to new replies.