Skip to content

Instantly share code, notes, and snippets.

@stankobrin
Created August 2, 2014 17:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stankobrin/bb0b7d38dbbe1fd3b83e to your computer and use it in GitHub Desktop.
Save stankobrin/bb0b7d38dbbe1fd3b83e to your computer and use it in GitHub Desktop.
Create new Wordpress post [wp_insert_post] based on results of a WP_Query
add_filter('uwpqsf_result_tempt', 'customize_output', '', 4);
function customize_output($results , $arg, $id, $getdata ){
// The Query
$apiclass = new uwpqsfprocess();
$query = new WP_Query( $arg );
ob_start(); $result = '';
// The Loop
if ( $query->have_posts() ) { ?>
<div class="orderresult_range">
<div class="range_title">
<?php $terms_as_text = strip_tags( get_the_term_list( $query->post->ID, 'ranges', '', ', ', '' ) ); echo $terms_as_text;?>
</div>
<?php
$range_image = get_field('range_image', $query->ID);
?>
<?php if( $range_image ): ?>
<img src="<?php the_field('range_image');?>" width="186" height="182" />
<?php else : ?>
<img src="<?php bloginfo('template_url'); ?>/assets/img/tempimage.jpg" width="186" height="182" />
<?php endif;?>
</div>
<div class="orderresultholder_title">
<table class="orderresult" width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>Code</td>
</tr>
<tr>
<td>Carats</td>
</tr>
<tr>
<td>Price</td>
</tr>
<tr>
<td>Qty</td>
</tr>
<tr>
<td></td>
</tr>
</table>
</div>
<?php while ( $query->have_posts() ) {
$query->the_post(); ?>
<div class="orderresultholder">
<table class="orderresult" width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td><?php the_field('style_code');?></td>
</tr>
<tr>
<td><?php $terms_as_text = strip_tags( get_the_term_list( $the_query->post->ID, 'sizes', '', ', ', '' ) ); echo $terms_as_text;?></td>
</tr>
<tr>
<td><?php $sellprice = get_field('selling_price', $query->ID); echo "R ".number_format($sellprice, 0);?></td>
</tr>
<tr>
<td>qty</td>
</tr>
<tr>
<td>
<?php
$order_productcode = get_field('product_code', $query->ID);
$order_productdesc = get_field('product_description', $query->ID);
$order_productcost = get_field('selling_price', $query->ID);
?>
<?php
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);
// This will redirect you to the newly created post
$post = get_post($post_id);
wp_redirect($post->guid);
}
?>
<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"/>
</form>
</td>
</tr>
</table>
</div>
<?php }
echo $apiclass->ajax_pagination($arg['paged'],$query->max_num_pages, 4, $id, $getdata);
} else { ?>
<?php
echo 'no post found';
} ?>
<?php
/* Restore original Post Data */
wp_reset_postdata();
$results = ob_get_clean();
return $results;
} ?>
@cpaul007
Copy link

cpaul007 commented Aug 2, 2014

I think that if statement have problem. try this once

if(isset($_POST['new_post']) && $_POST['new_post'] == '1')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment