Further to my previous question WP insert post PHP function and Custom Fields

We have a working insert post function that can also send over custom fields without yet knowing the ID. Please see the previous question for the answer. What we are trying to do now is have some of those custom fields dynamically load in. A session at the top of the page has loaded in all information from a post type called services. This is the code that then picks it up and inserts it into a new post.

$my_post = array(
    'post_title' => $_SESSION['booking-form-title'],
    'post_date' => $_SESSION['cal_startdate'],
    'post_content' => 'This is my post.',
    'post_status' => 'publish',
    'post_type' => 'booking',
$the_post_id = wp_insert_post( $my_post );

__update_post_meta( $the_post_id, 'duration', $_SESSION['booking-form-actual-duration'] );
__update_post_meta( $the_post_id, 'picture', $_SESSION['booking-form-picture'] );
__update_post_meta( $the_post_id, 'totalprice', $_SESSION['booking-form-total-price'] );
__update_post_meta( $the_post_id, 'activityduration', $_SESSION['booking-form-total-duration'] );
__update_post_meta( $the_post_id, 'totaldives', $_SESSION['booking-form-total-dives'] );

query_posts('post_type=services'); while (have_posts()) : the_post();
__update_post_meta( $the_post_id, 'producttitle'.get_the_ID(), $_SESSION['products-form-title'.get_the_ID()]); 
__update_post_meta( $the_post_id, 'productprice'.get_the_ID(), $_SESSION['products-form-price'.get_the_ID()]); 
__update_post_meta( $the_post_id, 'productduration'.get_the_ID(), $_SESSION['products-form-duration'.get_the_ID()]);
__update_post_meta( $the_post_id, 'productdives'.get_the_ID(), $_SESSION['products-form-dives'.get_the_ID()]);  
__update_post_meta( $the_post_id, 'productquantity'.get_the_ID(), $_SESSION['products-form-quantity'.get_the_ID()]); 

 endwhile; ?>

Notice the query posts and use of get_the_ID() to differentiate. The same thing occurred with the session at the top of the page to load in the data.

The code that then receives it is as follows. Notice that the reason for the IF statement is to only display the data of items that had quantity. Other php codes on the page retrieve the other custom posts.

<?php query_posts('post_type=services'); while (have_posts()) : the_post(); 
        $SPtitle = get_post_meta($post->ID, 'producttitle'.get_the_ID(), true);
        $SPprice = get_post_meta($post->ID, 'productprice'.get_the_ID(), true); 
        $SPduration = get_post_meta($post->ID, 'productduration'.get_the_ID(), true);
        $SPdives = get_post_meta($post->ID, 'productdives'.get_the_ID(), true);
        $SPquantity = get_post_meta($post->ID, 'productquantity'.get_the_ID(), true);
        if ( $SPquantity ) { 
        echo '<table width="478" border="0" cellspacing="5" cellpadding="0">
             <td width="50"><div style="border:1px solid #FFF;">',the_post_thumbnail(array(50, 50, true)),'</div></td>
             <td width="399" style="padding-left:14px;"><div class="textstandard_white"><span style="display:none;" id="QNTCON',get_the_ID(),'">',$SPquantity,'</span> ', $SPtitle,'</div><div class="textstandard_white" style="font-size:10px; color:#EEE">',the_excerpt(),'<input name="product_price_PRI_',get_the_ID(),'" id="product_price_PRI_',get_the_ID(),'" type="hidden" value="',$SPprice,'">
<input name="product_price_total_PRI_',get_the_ID(),'" id="product_price_total_PRI_',get_the_ID(),'" type="hidden" value="">
<input name="product_duration_PRI_',get_the_ID(),'" id="product_duration_PRI_',get_the_ID(),'" type="hidden" value="',$SPduration,'">
<input name="product_duration_total_PRI_',get_the_ID(),'" id="product_duration_total_PRI_',get_the_ID(),'" type="hidden" value="">
<input name="product_dives_PRI_',get_the_ID(),'" id="product_dives_PRI_',get_the_ID(),'" type="hidden" value="',$SPdives,'">
<input name="product_dives_total_PRI_',get_the_ID(),'" id="product_dives_total_PRI_',get_the_ID(),'" type="hidden" value="">

<input name="product_quantity_PRI_',get_the_ID(),'" id="product_quantity_PRI_',get_the_ID(),'" type="hidden" title="" value="',$SPquantity,'"/></div></td>
else {
echo '';
                <?php endwhile; ?>

Right so I hope that explains what I am trying to do. And may I point out that it is all functional with the exception of the query area services. Hence why I have provided you with that segment of code. Does anybody know why that bit isn't working.

Marvellous, Thanks.

  • first , after using the_post() you can use $post->ID instead of each get_the_ID() and save some load time. now i think you need to explain what you are getting and what are you trying to do.
    – Bainternet
    Feb 5 '11 at 17:55
  • I'm struggling to understand your use-case. Can you elaborate on what you mean by "send over custom fields"? Why are you updating all posts in one loop? Are you creating a web service? Why are you using sessions? The WordPress community avoids sessions if possible because of how it makes scaling much harder. It sounds like you might only need a global variable instead of a session? And rather than lots of get_post_meta() maybe use get_custom_fields($post_id)? And capture the value of get_the_ID() instead of repeated calls? Feb 5 '11 at 17:55
  • It is for a option based booking system. The code is all about finding all possible options 'services' and then rendering only those that have quantity as determined by the previous form. If you would like to see it in action. Go to divethegap.com/update/diving-trips/adventure-diving click on BEGINNERS, choose a start date, click CONTINUE, register and you will see exactly what I mean. Feb 5 '11 at 18:22
  • 404 not found
    – hakre
    Feb 5 '11 at 20:15
              $thispostID = $post->ID

              query_posts('post_type=services'); while (have_posts()) : the_post();

Needs to get the post id of the master post before querying the services.

Change all references to $post->ID to $thispostID

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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