Support

Account

Home Forums Front-end Issues Frontend Form Titles

Solving

Frontend Form Titles

  • I’m able to get the title to change on an ACF CPT on the backend in my functions.php file, but on the frontend the title shows up as (no-name). The title will not actually change, unless I open the CPT on the back end, click update and then it changes for both the Back and Front end. Any thoughts?

    my code is simple and one found in these forums before, but here it is:

    function my_cpt_title_update( $post_id, $post, $update ) {
    $title = array();
    if( get_post_type() == 'cpt' ) {
    $title = get_field('acf_variable', $post_id);
    }
    $content = array(
    'ID' => $post_id,
    'post_title' => $title
    );
    
    remove_action('save_post','my_cpt_title_update', 10, 3);
    wp_update_post( $content );
    add_filter('save_post', 'my_cpt_title_update', 10, 3);
    
     }
     add_action('save_post', 'my_cpt_title_update', 10, 3);

    It would great to make this work as I plan to make this CPT a relatioship field and the user can Add New and choose the actual name of the CPT and not (no-name) in the choices.

  • 
    function my_cpt_title_update( $post_id, $post, $update ) {
    $title = array();
    if( get_post_type() == 'cpt' ) {
    $title = get_field('acf_variable', $post_id);
    }
    $content = array(
    'ID' => $post_id,
    'post_title' => $title
    );
    
    remove_action('acf/save_post','my_cpt_title_update', 20);
    wp_update_post( $content );
    add_filter('acf/save_post', 'my_cpt_title_update', 20);
    
     }
     add_action('acf/save_post', 'my_cpt_title_update', 20);
    
  • Hi John,

    I appreciate your quick response. I did try ‘acf/save_post’; however this gives me an ArgumentCountError, the ACF documentation shows that acf/save_post does not work with the $post and $update parameters.

    I’ve tried removing them but then I can’t update the post on the frontend. I’m close and will keep experimenting, but if you have any other suggestions, I’m all ears.

  • I can explain the issue you are having.

    ACF saves values on the save_post hook. ACF’s acf/save_post hooks runs at a priority of 10. What this means is that your action

    add_action('save_post', 'my_cpt_title_update', 10, 3);

    is running before acf has saved the values. You might think that this is working in the admin but what should be happening is that you’re updating the title with the old value of the field and not the new value if it’s being changed.

    It’s not working at all on the front end because there is not “old” value as it’s a new post. Your action is running before ACF has saved it.

    You can try increasing the priority of your action to something >10, but I’m not sure that will work.

  • Again thank you John. This is a side project and I can’t dedicate a lot of time to it now, but soon I’m going to break down the entire code and start over.

    You were right that even a greater priority causes the same issue. I’ve tried using something like the below too:

    if(isset($_POST['acf']['field_6001b1b8a2cff']) ) {
          $new_title = $_POST['acf']['field_6001b1b8a2cff'];
        }

    But even then it works only on the backend since the front end still thinks there is no “old” value. I’ll figure this out eventually and post my results.

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

You must be logged in to reply to this topic.