Skip Navigation

[Resolved] change post type after form submission

This support ticket is created 3 years, 10 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 5 replies, has 2 voices.

Last updated by Lara 3 years, 10 months ago.

Assisted by: Christian Cox.

Author
Posts
#1952235

Tell us what you are trying to do?

I have a CRED form. If the custom select field "Event" is equal to 2, then nothing should happen and the post should be saved as 'appointment' post type. However if the custom select field "Event" is equal to 1, then post type should be changed into 'event' post type. I use cred_save_data hook and set_post_type()

The good thing is, that after form submit and under the condition, that the custom select field "Event" is equal to 1, the post type of the newly submitted post changes to 'event'.

The bad thing is, that I receive a critical WordPress error message after the form submission. It won't redirect me to the original page. WordPress doesn't seem to like the combination of "cred_save_data hook and set_post_type()" ...

How can I avoid the error message?

Code below..

add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data) {
  $forms = array( 123 );
  if ( in_array( $form_data['id'], $forms ) )
  {

if ($_POST['wpcf-event']['select'] == "1")) {

set_post_type( $post_id, $post_type = 'event' );

}

}
}

Is there any documentation that you are following?
no

Is there a similar example that we can see?
no

What is the link to your site?
Site under development

#1952623

Hello, I see some potential issues in the custom code.
1. There is an extra closing parenthesis on line 7, it should be deleted.

2. If the event field is a select field created by Types, you should test the selected value like this:

if ($_POST['wpcf-event'] == "1") {

This nested array element does not exist, and will throw an error:

$_POST['wpcf-event']['select']

3. Be sure to change 123 in your custom code to match this Form ID. It looks like you may have some test information here, just want to be sure you have the correct Form ID number in your actual code.

4. The correct syntax for set_post_type is as follows:

set_post_type( $post_id, 'event' );

Don't pass a variable assignment in the second param, just the post type slug as a string.

Try those adjustments and let me know the results, I may need to take another look at this.

#1954335

Hi Christian, many thanks for your answer.
I changed everything according to your suggestions.

add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)   {

  $forms = array( 10440 );
  if ( in_array( $form_data['id'], $forms )  {
 
if ($_POST['wpcf-event'] == "1")) {
 
set_post_type( $post_id, 'event' );
 
}
}
}

It performs the action that it should perform (change the post type), but it still throws the error message... Could it be that cred_save_data is the wrong hook?

#1954371

I think you removed the wrong closing parenthesis. Here is your code:

add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)   {
 
  $forms = array( 10440 );
  if ( in_array( $form_data['id'], $forms )  {
  
if ($_POST['wpcf-event'] == "1")) {
  
set_post_type( $post_id, 'event' );
  
}
}
}

Here is the corrected code:

add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)   {
 
  $forms = array( 10440 );
  if ( in_array( $form_data['id'], $forms ) )  {
  
if ($_POST['wpcf-event'] == "1") {
  
set_post_type( $post_id, 'event' );
  
}
}
}
#1954473

Yes you are right. I accidently removed the wrong closing parenthesis. I changed it, but the problem persist.
I even tried:

add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)   {
  
$forms = array( 10440 );
if ( in_array( $form_data['id'], $forms ) )  {

set_post_type( $post_id, 'event' );

}
}

I was curious, so I also tried:

add_action('cred_submit_complete', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)   {
  
$forms = array( 10440 );
if ( in_array( $form_data['id'], $forms ) )  {

set_post_type( $post_id, 'event' );

}
}

... and it works 🙂
Many thanks for your suggestions Christian.

#1954477

My issue is resolved now. Thank you!

add_action('cred_submit_complete', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)   {
   
$forms = array( 10440 );
if ( in_array( $form_data['id'], $forms ) )  {
 
set_post_type( $post_id, 'event' );
 
}
}