Skip Navigation

[Resolved] Can we change post_type in a CRED form?

This support ticket is created 5 years, 8 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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 8 replies, has 2 voices.

Last updated by davidM-25 5 years, 7 months ago.

Assisted by: Luo Yang.

Author
Posts
#1093651

We have a large CMS that has a big set of docs.

We have a post type for "docs".

A post type for contacts - by products, which has relationships with docs.

Another post type for corporate partners which again has relationships with docs.

And lastly a "ticket" post type that people use to submit problems, a possible new doc, new contact or whatever.

An example of a problem is that "ticket" has a couple of shared fields with "docs". So someone might fill out a ticket with 7 fields and most of the time this would not be converted to the "doc" post type but sometimes it is. Right now we have people going into the backend to change this. Not a big deal for our admin team but if we are handing over these duties to our general publishers... they do not touch WP admin side. So we simply need to be able to adjust the post_type field on the front end. Welcome to other suggestions too.

#1093700

Hello,

There isn't such a built-in feature within Toolset forms, I suggest you setup a Toolset form for editing "ticket" post, after user submit this form, use action hook "cred_save_data" to trigger a PHP function, in this custom function, use wp_update_post to change the post type to "docs".

More help:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
This hook allows doing a custom action when post data is saved to database.

https://developer.wordpress.org/reference/functions/wp_update_post/
Update a post with new post data.

#1094567

It is a little bit more complex than that. If I simply add a cred hook to the cred form then the form will process everything as a "doc" and not a ticket. This seems to be a pretty big deficiency in CRED right now since we are relying on the creating different post types to do different things. There seems like there should be a way to pass this off as a field as WP is saving it like that. Any additional help would be great as I think a lot of people would use this.

#1094654

As I mentioned above, there isn't such kind of built-in feature within Toolset form plugin:
There seems like there should be a way to pass this off as a field as WP is saving it like that
if you agree we can take it as a feature request, our developers will evaluate it.

I have already provide a workaround in above answer:
https://toolset.com/forums/topic/can-we-change-post_type-in-a-cred-form/#post-1093700

Please let me know if you still need assistance for it.

#1097339

That is the problem with the concepts I have come up with to solve this issue. Yes I can add a function to change the post type of the form - every single time you submit that form.

That does not solve my problem. I need the post types as a selection. As I mentioned the person editing the ticket most of the time will not change the post type. But they may. So having something that changes the cred save to a certain post type does not help unless I make everyone who wants to change it click on a special form which seems rather confusing.

Can we at least get this going as a request for a new feature?

#1098475

Yes, you can setup a custom generic field in your Toolset form for editing post, for example:

[cred_generic_field field='my_post_type' type='radio' class='' urlparam='']
{
"required":0,
"validate_format":0,
"default":["[wpv-post-type]"],
"options":[
{"value":"cpt-1","label":"CPT 1"},
{"value":"cpt-2","label":"CPT 2"},
{"value":"cpt-3","label":"CPT 3"}
]
}
[/cred_generic_field]

It will display as a radio field in your form, with options:
- CPT 1
- CPT 2
- CPT 3

when users submit the form, it will pass parameter "my_post_type" to form,

Then you can use it in action hook "cred_save_data", for example, add below codes in your theme file "functions.php":

add_action('cred_save_data', 'change_post_type_func',10,2);
function change_post_type_func($post_id, $form_data)
{
    // if a specific form
    if ($form_data['id']==123 && isset($_POST['my_post_type']))
    {
        $my_post = array(
			'ID'	=> $post_id,
			'post_type'	=> $_POST['my_post_type']
		);
		wp_update_post( $my_post );
    }
}

Please replace 123 with your Toolset form ID

#1100234

Thank you so much. That was a really good start and I totally forgot about adding generic fields to use with cred save. It would still be really great if Toolset would add this feature or give some information on how to add both post_type and _views_template to hidden fields.

I noticed that once I changed the post type that it did not default to that post type's default content template so I had get that saved too. Then it would default to the cred form's save action, and that brings up a CRED error because of the form to type mismatch so that had to be changed too. Below is what we are using until now. Not sure if I just want to hardcode in the views template ID yet.

add_action('cred_save_data', 'change_post_type_func',10,2);
function change_post_type_func($post_id, $form_data)
{
    // if a specific form
    if ($form_data['id']=='48612' && isset($_POST['my_post_type']))
    {
        $my_post = array(
            'ID'    => $post_id,
            'post_type' => $_POST['my_post_type'],
            '_views_template' => $_POST['my_views_template']
        );
        
       
        wp_update_post( $my_post );
    }
    
     
}

add_filter('cred_success_redirect', 'custom_redirect1',10,3);
function custom_redirect1($url, $post_id, $form_data)
{
     if ($form_data['id']=='48612' AND $_POST['my_post_type']=='source-content')
        return '<em><u>hidden link</u></em>';
    return $url;
}
#1100305

Yes, it is expected result:
brings up a CRED error because of the form to type mismatch so that had to be changed too

As I mentioned above, there isn't such a built-in feature within Toolset form plugin, we can take it as a feature request, our developers will evaluate it.
https://toolset.com/forums/topic/can-we-change-post_type-in-a-cred-form/#post-1094654

For new new question:
Not sure if I just want to hardcode in the views template ID yet.

You don't need to hardcode in the views template ID, you can setup the same content template for both post type, use [wpv-conditional] shortcode to check current post's post type [wpv-post-type], and display different Toolset form for user editing the post.

#1102299

Thank you for your help. I am going to test out using the post type shortcode in conditions. If that works it would be great. I would have some really really long templates but would work better.

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