Create new WordPress post [wp_insert_post] based on results of a WP_Query

Thanks to @Chinmoy Kumar Paul for his amazing help last night and for setting me on the right track.

The issue was that because the code was being run inside an AJAX results DIV, WordPress couldn’t locate the ID correctly. It was necessary to post the data using AJAX too and this works fine.

For anyone else looking to implement AJAX front-end posting – I found this post invaluable:
http://code.tutsplus.com/tutorials/adding-posts-to-a-sites-front-end-using-ajax–wp-25652

The last remaining item was to pass the variables from the wp_query to the form itself and then add the posts.

I did this by creating a hidden form field and echo’ing the data into the value field:

<input type="hidden" class="orderresult_qty" id="apfdesc" name="apfdesc" value="<?php echo the_title() ;?>"/>

I then passed the value of the field to a variable:

$orderdesc = $_POST['apfdesc'];

And then finally updated the add_post_meta data with the variable:

add_post_meta($post_id, 'product_description', $orderdesc, true);

Solution works 100%!

Leave a Comment