I have a function using the WP "save_post", that updates some field when a CTP is updated.
When I create this CTP through a Cred Form this function is not triggered.
Is it possible to trigger this function when a Cred form is submitted?
add_action( 'save_post', 'update_scheda_cliente',100,3 );
function update_scheda_cliente($id, $post, $isupdate) {
if ($post->post_status == 'publish' && $post->post_type =='cliente') {
global $wpdb;
$table_name = "wpaa_amelia_users";
$cid = $post->post_author;
$name = $wpdb->get_var("SELECT firstName FROM " . $table_name . " WHERE externalId=" . $cid);
/* update Customers post */
update_post_meta($id, 'wpcf-numero-cliente',$cid);
update_post_meta($id, 'wpcf-nome',$name);
// Change Customer post name
$title = get_the_title( $id );
// First, we look to see if title is not correct
if ( $title !== $fullname ) {
// We unhook this action to prevent an infinite loop
remove_action( 'save_post', 'update_scheda_cliente' );
// and we update the title to be the fullname
$args = array('ID'=> $id, 'post_title' => $fullname);
wp_update_post( $args );
// Now re-hook the action
add_action( 'save_post', 'update_scheda_cliente' );
}
}
}
The save_post action does fire when you publish posts using a front-end form made with Toolset Forms, but perhaps not how you expect.
As soon as you load the page with the form a draft post is created (with a custom 'auto-draft' post status).
Then when the form is submitted this draft post is updated, and its status changed to publish.
It's not obvious why your code above wouldn't work when the post is published via a front-end form instead of in the back end, though.
I suggest you first verify that the code is running by adding some messages to your debug log to the code so that you can see if the code is triggered, e.g.
I was able to narrow down the problem, but I still don't understand it.
The Cred form is set to create a Private CPT post, because I wanted only the creator of this post to be able to read it (apart of admins of course).
But somehow the function only works when the post is set to "Published".
As a workaround I could use Access, but I don't get the full logic of it.
I have set the custom post privileges to "Publish, Delete Own, Edit Own" hidden link
But still I see the other customers post as well hidden link
Ooops, my mistake!
Just wanted to exclude Draft posts, didn't though that "Private" was one of the possible post status.
Now the function works and create a Private post, but these posts don't show up in my Views anymore.
I have only a "Select posts with the author the same as the current logged in user." filter enabled.