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 badge1 Answer
Reset to default 0I'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
版权声明:本文标题:Custom post type: "transition_post_status" action get title and other fields 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736288703a1928147.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论