Support

Account

Home Forums General Issues acf/update problem with custom content type

Solving

acf/update problem with custom content type

  • Hello,

    I am building a site for our local hockey league. I’ve created a new Custom Content Type to show info about each game coming up (game_preview). In this, I’ve created 3 ACD fields:

    Home Team (Select) – home_team
    Away Team (Select) – away_team
    Game Date (Date) – game_date

    What I want to do is create a new post title that looks like this:
    home_team @ away_team – game_date

    However, all I can get is an “@” sign or the word “value” showing up, depending how I tinker with it. Can anyone check the code below for me and see what I am doing wrong?

    
    //ACF Title Override
    function game_preview_custom_title($value, $post_id, $field ){
    
    	$home = get_field("home_team");
    	$away = get_field("away_team");
    	$date = get_field("game_date");
    
    	$title = $home." @ ".$away." - ".$game_date;
    	$slug = sanitize_title($title); 
    
    	$previewdata = array(
        	'ID'          => $post_id,
        	'post_type'   => 'game_preview',
       	 	'post_title'  => $title,
    	  	'post_name'   => $slug
      	);
    
    	wp_update_post($previewdata);
    
    	return value;
    }
    
    add_filter('acf/update_value/name=home_team', 'game_preview_custom_title', 10, 3);
    add_filter('acf/update_value/name=away_team', 'game_preview_custom_title', 10, 3);
    add_filter('acf/update_value/name=game_date', 'game_preview_custom_title', 10, 3);
    
  • Hi @suavedan,

    Thanks for the post.

    I would recommend you make use of the acf/save_post action instead and then update the post title like so:

    //Auto add and update Title field:
      function my_post_title_updater( $post_id ) {
    
        $my_post = array();
        $my_post['ID'] = $post_id;
            
            $home = get_field("home_team");
    	$away = get_field("away_team");
    	$date = get_field("game_date");
    
    	$title = $home." @ ".$away." - ".$game_date;
    	$slug = sanitize_title($title); 
    
    	$previewdata = array(
        	'ID'          => $post_id,
        	'post_type'   => 'game_preview',
       	 	'post_title'  => $title,
    	  	'post_name'   => $slug
      	);
    
    	wp_update_post($previewdata);
    
      }
       
      // run after ACF saves the $_POST['fields'] data
      add_action('acf/save_post', 'my_post_title_updater', 20);
    
Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘acf/update problem with custom content type’ is closed to new replies.