我将重力表单与名为“cursussen”的自定义帖子类型结合使用。我制作了一个模板表单,当创建新的自定义帖子时,我会复制并更新它。我想根据从后端创建自定义帖子时输入的输入更新一些表单字段。
“一切”都正常。。但我唯一不能做的就是在表单中更新正确的值。下面您可以看到我的代码示例
// 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();
  }
}
 我使用此操作
add_action( \'transition_post_status\', \'create_new_form\', 10, 3);
 我认为问题在于$post对象。。这是因为该操作是在保存所有post数据之前触发的吗?
非常感谢!
 
                SO网友:Bram Huisman
                我想出了自己的解决办法。我使用了publish\\u post操作,而不是transition\\u post\\u status操作。
$this->loader->add_action( \'publish_cursussen\', $plugin_admin, \'save_cursus_meta\', 10, 2);
 以前的问题是尚未保存数据。。使用这个新函数,我可以获得ID和标题
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;
        }
    }
 希望这能对将来的人有所帮助!