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!

    • Elliot

    • January 28, 2015 at 10:41 am

    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);
    
Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.