Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial


Boris Kamp
Boris Kamp
16,660 Points

Create/update post title from ACF fields

Hi guys!

I've this code in order to successfully update the post title of my manufacturer and product post types:

//Auto add and update Title field:
  function my_post_title_updater( $post_id ) {

    $my_post = array();
    $my_post['ID'] = $post_id;

    $manufacturer = get_field('manufacturer');
    $target_product = get_field('target_product');

    $manufacturer_target = get_field('manufacturer', $target_product);

    if ( get_post_type() == 'manufacturer' ) {
      $my_post['post_title'] = get_field('manufacturer_name');
    } elseif ( get_post_type() == 'products' ) {
      $my_post['post_title'] = get_field('kitName') . ' (' . get_field('manufacturer_name', $manufacturer->ID) . ' ' . get_field('kitNumber') . ')';
    } elseif ( get_post_type() == 'reviews' ) {
       $my_post['post_title'] = get_field('kitName', $target_product->ID) . ' (' . get_field('manufacturer_name', $manufacturer_target->ID) . ' ' . get_field('kitNumber', $target_product->ID) . ')';

    // Update the post into the database
    wp_update_post( $my_post );


  // run after ACF saves the $_POST['fields'] data
  add_action('acf/save_post', 'my_post_title_updater', 20);

However, my latest elseif statement fails miserably! for the reviews posts type im trying to concatenate: <ol> <li>the kitName field from a relationship object (product post type object) so thats one layer deep: review > product </li> <li>the manufacturer_name field from the manufacturer field (manufacturer post object) of the product relationship mentioned directly above here. so that one is two layers deep: review > product > manufacturer</li> <li>the kitNumber field from a relationship object (product post type object) so thats one layer deep: review > product </li> </ol>

with I get the following error: Trying to get property of non-object. So basically it's telling me it expect an object but gets something else and thus cannot process the function, however, i have the return format of the relationship set to object, so I have no idea what I'm doing wrong here!? can anybody help me out please? thanks a lot!