Support

Account

Home Forums General Issues Create/update post title from ACF fields

Solved

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

    I highly suggest that you debug your code line by line:
    http://www.advancedcustomfields.com/resources/how-to/debug/

    It is most likely that the $target_product variable is not actually a single post object, but an array of them. This would explain the error.

    If you have trouble identifying the issue from your debugging, please post back the result of each variable and i”ll help point you in the right direction

    Cheers
    E

  • Elliot! you’re a hero!
    Stupid of me not to think about debugging first! You were right on point about that $target_product was not a single, but an array of post objects!
    so using this updated variable $review_target_product = $target_product['0']->ID; made my code work!

    For anyone interested in the future, this is all the code I used to construct my post titles in my functions.php

    
    //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');
        $review_target_product  = $target_product['0']->ID;
        $manufacturer_target    = get_field('manufacturer', $review_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', $review_target_product) . ' (' . get_field('manufacturer_name', $manufacturer_target) . ' ' . get_field('kitNumber', $review_target_product) . ')';
        }
    
        // 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);
    
  • Elliot,

    This may seem like a no brainer but to us coming up in skill levels, this was one of those that I struggled with for too long a time. I share this because I would hope this detail might gain more visibility for the sake of us who are learning.

    This bit here:

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

    is necessary for when you call:

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

    It’s not mentioned in the context of : Link:

    And although it’s more a WordPress related function than a ACF one, it’s crucial that the array handed to wp_update_post() includes the $my_post['ID'] = $post_id; bit in the array passed to it.

    Wordpress documentation the description in it’s examples:
    Link:

    Parameters #Parameters
    $postarr
    (array|object) (Optional) Post data. Arrays are expected to be escaped, objects are not. Default array.

    Default value: array()

    Doesn’t mention the necessity to pass the $post_id as: $myPostArray[‘ID’] assigned directly as that in that way. My mistake was to try and assign in this way:

    $myPostArray = array('post_id' => $post_id,
    'post_title' => $newTitle);

    This will fail with an error saying that the ID passed didn’t match.

    I spent no small amount of time figuring this out and the code example above is what made it make clear sense to me.

    It would be really great if this possible mistake was part of the documentation because it was really easy to make without the example above. It took me a good while to find this 😛

    Regards,

    -John

  • Hello,

    the problem with this solution is, that this triggers the update of the post two times:

    1. The actual change
    2. The wp_update_post() function to update the title based on an ACF field

    As a result, the revisions get two entries each time they are saved. Is there a way to prevent this?

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

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