Support

Account

Home Forums Backend Issues (wp-admin) Replacing custom post type post title with an acf? Reply To: Replacing custom post type post title with an acf?

  • Hi, thanks so much for this. Helped a lot! But I need some help here, please.

    I want my title to have not only the ACF field value, but also the custom taxonomy I’ve created called “pico”. When I run this code,

    function my_acf_update_value( $value, $post_id, $field ) {
    	
      $terms = wp_get_post_terms($post_id, 'pico');
    
      $pico = false;
      foreach($terms as $term)
      {
          if($term->parent)
          {
              $parent = get_term($term->parent, 'pico');
              $pico = $term->name;
              break;
          }
      }
      //Default to first selected term name if no children were found
      $pico = $pico ? $pico : $terms[0]->name;
    
    	$new_title = $pico . ' - ' . $value;
    	$new_slug = sanitize_title( $new_title );
    	
    	// Update post
      $my_post = array(
          'ID'           => $post_id,
          'post_title'   => $new_title,
          'post_name'		 => $new_slug,
      );
    
    	// Update the post into the database
      wp_update_post( $my_post );
    	
    	return $value;
    }
    
    add_filter('acf/update_value/name=data_do_boletim', 'my_acf_update_value', 10, 3);

    it gives me these error messages:

    Notice: Undefined offset: 0 in /Users/Mauricio/Sites/RicoSurf/wp-content/themes/ricosurf/functions.php on line 219

    Notice: Trying to get property of non-object in /Users/Mauricio/Sites/RicoSurf/wp-content/themes/ricosurf/functions.php on line 219

    Warning: Cannot modify header information – headers already sent by (output started at /Users/Mauricio/Sites/RicoSurf/wp-content/themes/ricosurf/functions.php:219) in /Users/Mauricio/Sites/RicoSurf/wp-includes/pluggable.php on line 1196

    But when I go back and see that post, the title is perfect, exactly the way I want.

    So I assume the code “is working” just not 100%. Can some one help me find out what’s wrong?