title: Wordpress call custom post save_post action from another function
body: <p>I have build a custom post in functions.php that works fine in the admin. Now I want to create a custom post manually from another php file using ajax. In functions.php I got something like this:</p>
  
  <pre><code>add_action('save_post', 'save_reference');
  
  function save_reference(){
      global $post;
      update_post_meta($post-&gt;ID, "company", $_POST["company"]);
      update_post_meta($post-&gt;ID, "contact", $_POST["contact"]);
      update_post_meta($post-&gt;ID, "address", $_POST["address"]);
      update_post_meta($post-&gt;ID, "zipcode", $_POST["zipcode"]);
      update_post_meta($post-&gt;ID, "town", $_POST["town"]);
      update_post_meta($post-&gt;ID, "phone", $_POST["phone"]);
      update_post_meta($post-&gt;ID, "mobile", $_POST["mobile"]);
      update_post_meta($post-&gt;ID, "email", $_POST["email"]);
  
      global $wpdb;
      $wpdb-&gt;query("UPDATE $wpdb-&gt;posts
  SET post_title = '$post-&gt;ID'
  WHERE ID = $post-&gt;ID");
  
  }
  </code></pre>
  
  <p>That one saves the post with the set post_metas and then updates the post title to be the actual post_id.</p>
  
  <p>I have setup the ajax to work and I'm setting all $_POST values manually at this point. In my ajax (which is set lower in functions.php) I try call save_reference(); but nothing happens. I got something like this:</p>
  
  <pre><code>$_POST['company'] = $json_array2[0];
  $_POST['contact'] = $json_array2[1];
  $_POST['address'] = $json_array2[2];
  $_POST['zipcode'] = $json_array2[3];
  $_POST['town'] = $json_array2[4];
  $_POST['phone'] = $json_array2[5];
  $_POST['mobile'] = $json_array2[6];
  $_POST['email'] = $json_array2[7];
  
  $_POST['post_type'] = 'reference';
  $_POST['content'] = 'test content';
  
  save_reference();
  </code></pre>
  
  <p>I prefer not to save the data with wp_query as I want to be able to call the save_reference(); as it should do the trick. I tried something with wp_insert too but with no success. Please help me.</p>
  
  <p>EDIT MY SOLUTION:
  In the ajax I used:</p>
  
  <pre><code>global $post;
  $post_ID = wp_insert_post(array('post_type' =&gt; 'reference', 'post_status' =&gt; 'publish', 'post_content' =&gt; "$_POST[content]"));
  $post = get_post($post_ID);
  save_reference();
  </code></pre>