admin管理员组

文章数量:1122832

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!

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!

Share Improve this question asked Jul 21, 2017 at 16:57 Bram HuismanBram Huisman 31 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 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!

本文标签: Custom post type quottransitionpoststatusquot action get title and other fields