Skip Navigation

[Resolved] Save_post function with Cred Form

This support ticket is created 3 years, 11 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+01:00)

This topic contains 8 replies, has 2 voices.

Last updated by Puntorosso 3 years, 11 months ago.

Assisted by: Nigel.

Author
Posts
#1636711

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' );	
	}	
}
}
#1637183

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

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.

add_action( 'save_post', 'update_scheda_cliente',100,3 );
function update_scheda_cliente($id, $post, $isupdate) {
    error_log("function called"); 
if ($post->post_status == 'publish' && $post->post_type =='cliente') {
     error_log("condition passed");
...

Can you first confirm that the code is run when submitting from the front-end...?

#1637547

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

How can I avoid that?

Thanks

#1638139

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

somehow the function only works when the post is set to "Published"

But, that's right there in your code:

if ($post->post_status == 'publish' && $post->post_type =='cliente') {

You only apply this to posts with a status of publish.

Did you try removing the test for the post status?

#1638361

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.

Are private posts not accessible through Views?

#1638495

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Screenshot 2020-05-26 at 14.31.55.png

You can add a Query Filter to a View for post_status where you specify exactly which statuses should appear in the results.

#1638705

Yes I know that.

But I have only a "Select posts with the author the same as the current logged in user." and no post status query filter is enabled.

In this case all posts of current user should be displayed, right?
I cannot see posts with status "Private" at the moment.

#1638707

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Yes, so add a Query Filter for post_status and include private posts. They are not included by default.

#1638781

That did the trick!
Thanks a lot Nigel for your patience.
Glad you're here!
Have a nice day.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.