Home Forums ACF PRO Create/update post title from ACF fields


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:

    1. the kitName field from a relationship object (product post type object) so thats one layer deep: review > product
    2. 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
    3. the kitNumber field from a relationship object (product post type object) so thats one layer deep: review > product

    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!

  • Hi @boriskamp1991,

    Thanks for the post.

    Within the acf/save_post it is highly recommended to get custom field values from post data like so: $_POST[‘acf’][‘field_77xujjsusi’]

    The second parameter should be the field key.

  • Thanks @acf-support
    I have the same in various groups, I have the same field called heading, I use this field to set the post title on 90% of my posttypes. using the field key would need me to define the key for each group, while if I do $my_post['post_title'] = get_field('heading'); it would the heading field on my contact field group, but also on my about field group for example.

    I’ve never had issues doing it this way.

    What’s you’re opinion about this?

  • Hi @boriskamp1991

    Could you please debug all of the possible fields and variables? This page should give you more idea about it: Also, could you please try to use the get_field() function one by one between the kitNumber, manufacturer_name, and the kitName fields? That way we will know which is the code at fault.

    Thanks 🙂

Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘Create/update post title from ACF fields’ is closed to new replies.