title: programmatically insert child post into Wordpress
body: <p>I need to programmatically create a post (or page) for each Wordpress post created or updated by a user via the dashboard.  I added a hook </p>
  
  <pre><code>add_action( 'publish_post', 'create_details_page');
  </code></pre>
  
  <p>An auto post is created only if the user creates or updates a post in a certain category, and the auto post is created in a different category. Each post belongs to just one category. 
  Create the post as follows: </p>
  
  <pre><code>        $auto_post = array(
                  'comment_status' =&gt; 'closed',
                  'post_category' =&gt; array($category-&gt;term_id),
                  'post_author' =&gt; $latest_post[0]-&gt;post_author,
                  'post_type' =&gt; 'post',
                  'post_title' =&gt; 'Details for ' . $latest_post[0]-&gt;post_title,
                  'post_parent' =&gt; $latest_post[0]-&gt;ID,
                  'post_content' =&gt; 'Post content'
          );
          $auto_post_id = wp_insert_post ( $auto_post, true );
          $details = get_post( $auto_post_id );
          wp_publish_post( $auto_post_id );
  </code></pre>
  
  <p>The results are inconsistent: sometimes I get one auto post created, sometimes two, and occasionally none.  Why, and how to insert post exactly once?</p>
  
  <p>To retrieve the auto post as a child of the user-created post: </p>
  
  <pre><code>$args = array(
          'post_type' =&gt; 'post',
          'post_parent' =&gt; $parent_post_id,
          'post_status' =&gt; 'publish'
          /* 'category_name' =&gt; array('Auto Post Category') */
  );
  $children = get_posts( $args );
  </code></pre>
  
  <p>Adding the category_name parameter causes to retrieve no child posts at all. Without the category parameter, child posts are returned, and they have the category set property. However, it seems like not a full list is retrieved, and results vary from run to run. </p>
  
  <p>If the content of an auto post is then edited from the dashboard, this edited post is not returned by the above query.   Why?</p>
  
  <p>Any suggestions on how to resolve the inconsistent behavior and get this working? 
  I am new to Wordpress, and online help is sparse and mostly aimed to the dashboard user.</p>
  
  <p>Using Wordpress 3.0.4, php5.   </p>