I have a a page where users can filter a list of custom post-type posts based on the "Ultimate WP Query Search Filter" plugin. Results are passed to a div on the page using AJAX from there users can enter a quantity into each of the results.

What I need to happen is that once a user has entered a quantity into the quantity field all the information from that post, plus the quantity need to be used to create a new post in a different custom post type. [Currently this is done a result by result basis, but if its possible to have 1 "add" button at the end that loops through each result and creates a post for each that would be first prize - but we are ok with an "Add" button per result]

I am using the following code to create the new posts:

    $order_productcode = get_field('product_code', $query->ID);
    $order_productdesc = get_field('product_description', $query->ID);
    $order_productcost = get_field('selling_price', $query->ID);

if(isset($_POST['new_post']) == '1') {
$post_title = $_POST['post_title'];

$new_post = array(
      'post_type'       => 'order-items',
      'ID'              => '',
      'post_author'     => $user->ID, 
      'post_title'      => $post_title,
      'post_status'     => 'publish'

$post_id = wp_insert_post($new_post);

add_post_meta($post_id, 'linked_order_id', 379, true);
add_post_meta($post_id, 'product_code', $order_productcode, true);
add_post_meta($post_id, 'product_description', $order_productdesc, true);
add_post_meta($post_id, 'quantity', '1', true);
add_post_meta($post_id, 'unit_price', $order_productcost, true);

$post = get_post($post_id);
wp_redirect($post->guid);} ?>      

And then the following code to trigger the form:

<form method="post" action=""> 
<input type="text" class="orderresult_qty" name="post_title" size="45" id="input-title"/>
<input type="hidden" name="new_post" value="1"/> 
<input class="subput round" type="submit" name="submit" value="Add"/>

On a single page or hard-coded page example this works perfectly, but when the code is placed inside of the Wordpress loop, nothing happens when the item is submitted. No error is displayed and the item is not created.

I am having problems pasting the full code, so here is a link to a GIST where you can see the full code inside the loop (generated inside the function.php file)


This is quite urgent so any assistance in getting this working will be appreciated!

  • I can provide links and access to the site if required.
    – stankobrin
    Aug 2, 2014 at 17:41
  • I think that if statement have problem.try this once if(isset($_POST['new_post']) && $_POST['new_post'] == '1') Aug 2, 2014 at 18:02
  • nope same outcome sadly :(
    – stankobrin
    Aug 2, 2014 at 18:11
  • add echo before this line $post_id = wp_insert_post($new_post); . After submitting the form if you get the new post ID then you will be confirmed that if statement is working. Otherwise there have some problem. Aug 2, 2014 at 18:17
  • same thing happens - page refreshes to the same page instead of redirecting user the created post. So does this mean the if statement is incorrect?
    – stankobrin
    Aug 2, 2014 at 18:27

1 Answer 1


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%!

  • WOW! Lastly you solved the issue. This is very good news for me. Aug 3, 2014 at 18:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.