Support

Account

Home Forums General Issues Change title and post name with form fields?

Helping

Change title and post name with form fields?

  • I’ve created a function to change the post title and post name using fields from a frontend form for a given custom post type.

    add_action('acf/save_post', 'chiwp_update_post_name', 20);
    
    function chiwp_update_post_name($post_id)
    {
      $desired_url = get_field('show_information', $post_id);
      $desired_url = $desired_url['desired_url_extension'];
      $title = get_field('show_information', $post_id);
      $title = $title['show_name'];
    
      $data = array(
        'ID'         => $post_id,
        'post_title' => $title,
        'post_name'  => sanitize_title($desired_url),
      );
    
      wp_update_post($data);
    }

    This function correctly updates the data as it should, however, there is an issue with saving. When I view the post preview in the admin, it displays the page as if no fields have been entered. Yet, The actual admin page shows that all of the field data is there, so we know the form works properly.

    If I simply click ‘save draft’ and then view the draft, everything displays properly.

    I think I just need to make sure the updated fields save as a draft AFTER updating. I’ve tried adjusting the priority, from 20 to 5 but that doesn’t seem to work.

    Here is the form:

    $fields = array(
                  'field_5d0d0b6322401',
                  'field_5d0d0bbc22403'
                );
                acf_register_form(array(
                  'id'       => 'new-things',
                  'post_id'  => 'new_post',
                  'new_post' => array(
                    'post_type'   => 'things',
                    'post_status' => 'draft',
                  ),
                  'post_title'   => false,
                  'post_content' => false,
                  'uploader'     => 'basic',
                  'return'       => home_url('/'),
                  'fields'       => $fields,
                  'submit_value' => 'Submit the thing'
                ));
    
                acf_form('new-things');

    Any ideas with this one? Thanks!

  • After many iterations of trying to fix this, I’m starting to think it’s a bug.

    I noticed that if I change 'post_status' => 'draft' to 'post_status' => 'publish' everything works fine.

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

The topic ‘Change title and post name with form fields?’ is closed to new replies.