0

I am using gravity forms in combination with a custom post type called: "cursussen". I've made a template form, which i duplicate and update when a new custom post is created. I want to update some form fields based on the inputs entered when the custom post is created from the back-end.

"Everything" works ok..but the only thing i can't get to work is getting the correct values updated in the the form. Below you can see a example of my code

// Listen for publishing of a new cursus post
function create_new_form($new_status, $old_status, $post) {
    $post_title = $post->title;
    //Check if post is published
  if('publish' === $new_status && 'publish' !== $old_status && $post->post_type === 'cursussen') {

        //Create form
        function vhh_add_form($form) {
            $form_id = 2; //Standard form
            $form = GFAPI::get_form($form_id);
            $form['title'] = $post_title;
            $result = GFAPI::add_form( $form );
            return $result;
        }
        vhh_add_form();
  }
}

I use this action

add_action( 'transition_post_status', 'create_new_form', 10, 3);

I think the problem lies in the $post object.. is this because the action is fired before all post data is saved?

Many thanks in advance!

0

I've come up with my own solution. I used the publish_post action instead of the transition_post_status action.

$this->loader->add_action( 'publish_cursussen', $plugin_admin, 'save_cursus_meta', 10, 2);

The problem before was the data not being saved yet.. with this new functions i can get the ID and with that ID the title

public function add_new_form( $cursus_title ){
        $form_id = 2; //Standard form
        $form = GFAPI::get_form($form_id);
        $form['title'] = $cursus_title;
        $form['limitEntriesCount'] = 100;
        $result = GFAPI::add_form( $form );
        return $result;
    }

    public function save_cursus_meta( $ID, $post ){
        $post_type = get_post_type($ID);
        $cursus_title = get_the_title($ID);
        // If this isn't a 'book' post, don't update it.
        if ( "cursussen" != $post_type) return;

        $myPost = get_post($ID);
        if( $myPost->post_modified_gmt == $myPost->post_date_gmt ){
            $this->add_new_form($cursus_title);
        }else{
            return;
        }
    }

Hope this can help somebody in the future!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.