• Resolved esedic

    (@esedic)


    I have created two post actions in a form.

    First action creates new custom post type and second action updates custom post type.

    The idea is that using acfe/form/prepare/post/ filter I want to under certain condition skip first action (create) and trigger second action (update).

    <?php
    
    function school_form_prepare($prepare, $form, $post_id, $action){
    
    // This a select field, I'm using selected option to get title of the saved/updated CPT
    $form_school_title = get_field('school_title');
    
    // get_page_by_post_name is my custom function to replace deprecated get_page_by_title()
    $school_post = get_page_by_post_name($form_school_title, OBJECT, 'school');
    
    $post_school_title = $school_post->post_title;
    
    if($form_school_title == $post_school_title){
        $prepare = false;    
    }
    
    // return
    return $prepare;
    
    }
    add_filter('acfe/form/prepare/post/form=school-form', 'school_form_prepare', 10, 4);


    I can’t get form to update existing post.

    Form is submitted successfully.
    Conditon if($form_school_title == $post_school_title) is working (I’ve tested it on acfe/form/validation/post hook to display a validition message, if conditon is true).

    Any help would be appreciated.

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

    (@hwk-fr)

    Hello,

    Thanks for the feedback!

    I would recommend to use acf_log() in your code to check that your variables are filled as expected during the form prepare/save phases.

    Example:

    add_filter('acfe/form/prepare/post/form=school-form', 'school_form_prepare', 10, 4);
    function school_form_prepare($prepare, $form, $post_id, $action){
    
    // This a select field, I'm using selected option to get title of the saved/updated CPT
    $form_school_title = get_field('school_title');
    
    acf_log('form_school_title:', $form_school_title);
    
    // get_page_by_post_name is my custom function to replace deprecated get_page_by_title()
    $school_post = get_page_by_post_name($form_school_title, OBJECT, 'school');
    
    $post_school_title = $school_post->post_title;
    
    acf_log('post_school_title:', $post_school_title);
    
    if($form_school_title == $post_school_title){
        acf_log('Stop prepare!');
        $prepare = false;    
    }
    
    // return
    return $prepare;
    
    }

    This will help you to check everything is in order. You’ll find a guide explaining how to debug WordPress & ACF here showing how its used.

    Additionally, note that $post->post_title might have different output than get_the_title(), for example with dash - becoming once formatted by WordPress in get_the_title().

    There might be other small differences like that which would make your condition not valid. Using logs on your variables will give you more information.

    Hope it answers your question.

    Regards.

Viewing 1 replies (of 1 total)
  • The topic ‘Trouble with prepare filter’ is closed to new replies.