Contact form 7 to custom post type

Kyle Poyser picture Kyle Poyser · Feb 6, 2017 · Viewed 10.1k times · Source

I would like to process a contact form from contact form 7 into a custom post type.

Currently, this is what I have:

<?php 

if ( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) &&        $_POST['action'] == "front_post") {

//store our post vars into variables for later use
//now would be a good time to run some basic error checking/validation
//to ensure that data for these values have been set
$title     = $_POST['title'];
$content   = $_POST['content'];
$Interest   = $_POST['Interest'];
$post_type = 'purchase';


//the array of arguements to be inserted with wp_insert_post
$new_post = array(
'post_title'    => $title,
'post_content'  => $content,
'tags_input'  => $tags,
'posted_data' => $Interest,
'post_status'   => 'publish',
'post_category' => array('0',$_POST['cat']),          
'post_type'     => $post_type 
);

//insert the the post into database by passing $new_post to wp_insert_post
//store our post ID in a variable $pid
//we now use $pid (post id) to help add out post meta data
$pid=wp_insert_post($new_post);

//we now use $pid (post id) to help add out post meta data
add_post_meta($pid, 'cust_key', $custom_field);


}
?>

Here is a link to the actual form: http://stage.icardpromotions.com/create-purchase-order/

I need to be able to pull in all of the info form this form into the custom post type "purchase"

As you can see, I am currently pulling in the post_content, post_title, etc.

I have also tried to pull in content from content form by input name "Interest" but it dose not work.

Does anyone have a clue how to do this?

Answer

sagar picture sagar · Apr 27, 2017
 function save_posted_data( $posted_data ) {


       $args = array(
         'post_type' => 'post',
         'post_status'=>'draft',
         'post_title'=>$posted_data['your-name'],
          'post_content'=>$posted_data['your-message'],
       );
       $post_id = wp_insert_post($args);

       if(!is_wp_error($post_id)){
         if( isset($posted_data['your-name']) ){
           update_post_meta($post_id, 'your-name', $posted_data['your-name']);
         }
        // if( isset($posted_data['your-email']) ){
        //   update_post_meta($post_id, 'your-email', $posted_data['your-email']);
        // }
        // if( isset($posted_data['your-subject']) ){
        //   update_post_meta($post_id, 'your-subject', $posted_data['your-subject']);
        // }
         if( isset($posted_data['your-message']) ){
           update_post_meta($post_id, 'your-message', $posted_data['your-message']);
         }
      //and so on ...
      return $posted_data;
     }
 }

add_filter( 'wpcf7_posted_data', 'save_posted_data' );

-------------------- Explaining It-------------------------

First make function and add a hook wpcf7_posted_data to it

---first step---

function save_posted_data( $posted_data ) {

}
add_filter( 'wpcf7_posted_data', 'save_posted_data' );

---second step---

and now u need to add some arguments to the post that needs to be populated using wp_insert_post();

$args = array(
         'post_type' => 'post',
         'post_status'=>'draft',
         'post_title'=>$posted_data['your-name'],
          'post_content'=>$posted_data['your-message'],
       );
$post_id = wp_insert_post($args);

---third step---

check if that populated items is error or not

if(!is_wp_error($post_id)){ //do ur stuffs }

---fourth step---

now checking isset the field or not and updating the metas eg post

if( isset($posted_data['your-name']) ){
    update_post_meta($post_id, 'your-name', $posted_data['your-name']);
}

and in last return the value

return $posted_data;

Full code is above.