• Resolved pzh20

    (@pzh20)


    I’m using the form I had problems with earlier, and I hide the Title and Content fields on the frontend. I’m then trying to use the ACF/save_post to check it’s the particular post type, and take twoi fields from the form and update the title field.

    The ACF Field Group is called ‘Font Pairings’. The code I’m using is as follows;

    add_action( 'acf/save_post' , 'backend_after_save_post', 5);

    function backend_after_save_post( $post_id ){
    //if( ! isset( $_POST['acf'] ) ){
    // return $_POST[‘_acf_post_id’];
    //}
    $posttype = get_post_type( $post_id );
    debug_to_console($posttype);
    if( get_post_type( $post_id ) == 'font parings' ) {

    $firstfont = get_field('font_name',$post_id);
    $pairfont = get_field('font_pair',$post_id);

    $post_title = implode(' ',array_filter([$firstfont,'-',$pairfont])) ;

    remove_action( 'acf/save_post', '_acf_do_save_post' );
    $edit_post = array( 'ID' => $post_id, 'post_title' => $post_title );

    $edit_post['post_name'] = sanitize_title( $post_title );
    wp_update_post( $edit_post );
    add_action( 'acf/save_post', '_acf_do_save_post' );
    }

    }

    Nothing is being changed, any ideas what I’m doing wrong?

    Many thanks

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author Konrad Chmielewski

    (@hwk-fr)

    Hello,

    I’m not 100% sure to understand what you’re trying to do, but I would rather recommend to use ACFE Form hooks if you need to retrieve field input values/update fields during the form or actions submission.

    For example, if you want to hook after the “Post Action” submission, you can use the acfe/form/submit_post hook (See documentation). This action will let you update fields on the newly created/updated post for example.

    You’ll find more examples in the documentation, and also global hooks (outside of actions) in the documentation here. You might also want to checkout the helpers documentation to understand how to get input values and or field DB values.

    The PHP Integration documentation will also give you some tips to hide fields, prefill fields, conditionally render a form etc… It might be worth to check out.

    Side note: If you want to use multiple fields as the Post Title of your “Post Action” you can simply type {field:font_name} - {field:font_pair} in the “Post Title” setting in the Form UI “Post Action”. This will use both fields values to generate the post title. See Template Tags documentation.

    Hope it helps!

    Have a nice day!

    Regards.

    Thread Starter pzh20

    (@pzh20)

    Thanks for getting back so quickly, just to confirm, the user isn’t adding a titl to the post, I’m taking 2 input fields and concattening them into the Title. As I have several different post types, I was checking which post type is being submitted as all submits go through the same hook.

    However, using your hook, and adding the form=’my-form-name’, I wouldn’t need to check the post type. So looking at the documentation, all I would need is;

     update_field('post_name', sanitize_title( $input_field1." - ".$input_field2 ), $post_id);

    where $input_field1 and 2 are fields completed on the form

    Can you clear up something, my post type is called ‘Font Parings’, does that get saved as ‘font-parings’ as I can’t seem to find it’s saved name.

    regards

    Pete

    Plugin Author Konrad Chmielewski

    (@hwk-fr)

    Hello,

    Please note there is difference between Custom Post Meta (ACF fields), and WP_Post fields (post_title, post_name, post_content etc…).

    When you use update_field(), you update a custom post meta (ie: my_post_meta). If you want to update the actual Post Title or Post Name (slug) of a post, you have to update the WP_Post object.

    If you want to concatenate two fields to generate the WP_Post Post title or Post name, you can simply type in the Form UI “Post Action” – Title: {field:my_field_1} - {field:my_field_2}. See screenshot.

    If you prefer to do it with PHP code, then you can use the acfe/form/submit_post_args hook (see documentation), which allows you to change the WP_Post fields right before the post is created/updated in the database with wp_update_post() behind the scene. Usage example:

    add_filter('acfe/form/submit_post_args/form=my-form', 'my_post_submit_args', 10, 3);
    function my_post_submit_args($args, $form, $action){

    // change post title
    $args['post_title'] = get_field('my_field_1') . ' - ' . get_field('my_field_2');

    // return
    return $args;

    }

    If you want to concatenate fields and update a post meta, then yes you have to use update_field() in acfe/form/submit_post/form=my-form.

    Note that you can target a specific action name with this hook, if you have multiple “Post Action” in the same form, by naming the “Post Action” in the UI and using acfe/form/submit_post/action=my-post-action-name.

    In all cases, you have the $post_id of the created/updated post as the first argument, which allows you to get the post type with get_post_type($post_id) for example.

    add_action('acfe/form/submit_post/form=my-form', 'my_post_submit', 10, 4);
    function my_post_submit($post_id, $args, $form, $action){

    // get the post type of the post that has been created/updated
    $post_type = get_post_type($post_id);

    if($post_type === 'my-post-type'){

    // concatenate fields
    $value = get_field('my_field_1') . ' - ' . get_field('my_field_2');

    // manually update a field on the created/updated post
    update_field('my_custom_field', $value, $post_id);

    }

    }

    Hope it helps!

    Regards.

    Thread Starter pzh20

    (@pzh20)

    I’m getting to like this pugin more and more. However, I followed your suggestion of handling the post title by concatenating the fields in the form settings, but it generated a title of (field: font_family) – (font_pair) as in this screen shot instead of the field values

    Image here as I can seem to upload an image

    https://tinyurl.com/28je6agf

    Plugin Author Konrad Chmielewski

    (@hwk-fr)

    Please be careful with the syntax, ACFE Template Tags use braces {}, not parenthesis ().

    Also make sure to not add any space between the colon : and the field name. See Template Tags documentation.

    Also see my example above:

    You can simply type in the Form UI: {field:my_field_1} - {field:my_field_2}

    Regards.

    Thread Starter pzh20

    (@pzh20)

    Sorrry, too long staring at a computer screen!

    Thanks

Viewing 6 replies - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.