Navigation überspringen

[Gelöst] Post and related custom post type on same front end form

Dieser Thread wurde gelöst. Hier ist eine Beschreibung des Problems und der Lösung.

Problem:
Post and related custom post type on same front end form

Solution:
You should use the Toolset form's hook "cred_save_data" and then use the wp_insert_post() function to create a new entry automatically and then use the post relationship API function toolset_connect_posts().

You can find the proposed solution in this case with the following reply:
https://toolset.com/forums/topic/post-and-related-custom-post-type-on-same-front-end-form/page/2/#post-1354943

Relevant Documentation:
=> https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
=> https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_connect_posts

This support ticket is created vor 5 Jahren, 3 Monaten. 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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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/Kolkata (GMT+05:30)

Dieses Thema enthält 19 Antworten, hat 2 Stimmen.

Zuletzt aktualisiert von Daniel Stadeli vor 5 Jahren, 3 Monaten.

Assistiert von: Minesh.

Author
Artikel
#1354873

YEEESSS! That's it. I added a couple missing fields and saved it, you are good to go.

The generic fields at the end are styled differently than the regular post type fields, but I can fix that later I would assume.
I have inserted the form shortcode here, so you can test your code: versteckter Link

#1354943

Minesh
Supporter

Sprachen: Englisch (English )

Zeitzone: Asia/Kolkata (GMT+05:30)

Can you please check now:

I've added the following code to "Custom Code" section offered by Toolset "custom-code1":
=> versteckter Link

//Create a dynamic post title by the CRED form.
add_action('cred_save_data','func_combine_field_entries',10,2);
function func_combine_field_entries($post_id,$form_data) {
  
    if ($form_data['id']==1243) {
         				 
      
                        
                        $buch = $_POST['wpcf-bibelbuch'];
        				$chapter = $_POST['wpcf-chapter2'];  
						$verse =  $_POST['wpcf-verse-from'];  
						$verse_to =  $_POST['wpcf-verse-to2'];    
                        //one verse or range?
                        $range = $verse;
                        if (!empty($verse_to)) {
                            $range .= '-'.$verse_to;	
                        };
						//concatenate title
						$child_title= $buch. ' ' . $chapter .','. $range; 
                        $child_slug = $post_id.'-'.$child_title;
                          
                        // Child Post Args
                        $child_post_args = array(
                          'post_title'  => $child_title ,
                          'post_name'  => $child_slug ,
                          'post_status'   => 'publish',
                          'post_type'   => 'bible-verses', // Here you can put your child post type slug
                        );
  
                        // Insert the post into the database
                        $child_post_id = wp_insert_post( $child_post_args );
                        if( isset( $child_post_id ) && $child_post_id ) {
                                           update_post_meta($child_post_id, 'wpcf-bibelbuch',$_POST['wpcf-bibelbuch']);
        				   update_post_meta($child_post_id, 'wpcf-chapter2',$_POST['wpcf-chapter2']);
						   update_post_meta($child_post_id, 'wpcf-verse-from',$_POST['wpcf-verse-from']);
						   update_post_meta($child_post_id, 'wpcf-verse-to2', $_POST['wpcf-verse-to2']);
                          
                           // connecting child post with parent
                           toolset_connect_posts( 'post-bible-reference', $post_id,  $child_post_id );
                        }
    }
}

And now I can see parent and child entry created automatically and connected as well.

The only thing you need to do is - you need to add all the select options for the field "Buch" you added here:
=> versteckter Link

You need to add the same options to your Generic select field "wpcf-bibelbuch". I've added 3 options, you need to add all remaining one.
Please check the following screenshot:
=> versteckter Link

Gald to help and finally happy to see your issue is resolved 🙂

#1354977

This is great Minesh, you are a hero!! The form combination works and it saves to both post types - this is great.
This project calls for multiple bible verses to be added to one post. Is there a way to do that with like a generic repeater field?

Or would I have to put several copies of these fields on the form? I would have to put a hard limit on this relationship, which is not the intend, but still better than only having one bible reference:

1: Book Chapter Verse VerseTo
2: Book Chapter Verse VerseTo
3: Book Chapter Verse VerseTo
4: Book Chapter Verse VerseTo
5: Book Chapter Verse VerseTo

(It looks like a nightmare to enter all those bible books 5 times in the 5 different select fields)

#1354987

Minesh
Supporter

Sprachen: Englisch (English )

Zeitzone: Asia/Kolkata (GMT+05:30)

This project calls for multiple bible verses to be added to one post. Is there a way to do that with like a generic repeater field?
==>
No, there is no such option/feature available.

Or would I have to put several copies of these fields on the form? I would have to put a hard limit on this relationship, which is not the intend, but still better than only having one bible reference:
==> Yes, exactly, you need to add all child post fields 5 times if you want to create 5 entries at a time and you need to adjust the code accordingly.

#1355011

My issue is resolved now. Minesh, you were most helpful. Thank you!