How to save contact form 7 data in Custom Post Types (CPT)

The question:

I want to store the data of contact form 7 (CF7) in a custom-post-types (CPT).
There are several plugins like:

which store the data in their own cpt. But it is not possible to redirect the data into a CPT of your own.

Is there a plugin or another way how I can store the data from CF7 in a CPT of a third plugin?

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

If you want to save the Mail Content after the Contact Form was sent (or failed), you can use the wpcf7_mail_sent and wpcf7_mail_failed Hookes like this:


function save_my_form_data_to_my_cpt($contact_form){
    $submission = WPCF7_Submission::get_instance();
    if (!$submission){
    $posted_data = $submission->get_posted_data();
    //The Sent Fields are now in an array
    //Let's say you got 4 Fields in your Contact Form
    //my-email, my-name, my-subject and my-message
    //you can now access them with $posted_data['my-email']
    //Do whatever you want like:
    $new_post = array();
    if(isset($posted_data['my-subject']) && !empty($posted_data['my-subject'])){
        $new_post['post_title'] = $posted_data['my-subject'];
    } else {
        $new_post['post_title'] = 'Message';
    $new_post['post_type'] = 'my_awesome_cpt'; //insert here your CPT
        $new_post['post_content'] = $posted_data['my-message'];
    } else {
        $new_post['post_content'] = 'No Message was submitted';
    $new_post['post_status'] = 'publish';
    //you can also build your post_content from all of the fields of the form, or you can save them into some meta fields
    if(isset($posted_data['my-email']) && !empty($posted_data['my-email'])){
        $new_post['meta_input']['sender_email_address'] = $posted_data['my-email'];
    if(isset($posted_data['my-name']) && !empty($posted_data['my-name'])){
        $new_post['meta_input']['sender_name'] = $posted_data['my-name'];
    //When everything is prepared, insert the post into your WordPress Database
    if($post_id = wp_insert_post($new_post)){
       //Everything worked, you can stop here or do whatever
    } else {
       //The post was not inserted correctly, do something (or don't ;) )

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment