aleks1217
aleks1217

Reputation: 87

How to run an additional calculation on update_post-meta

I have a custom post type where I want to output two meta boxes ("delivery_date" and "orderby_date") based on a date I put in a custom field called Delivery Date. In addition to having the delivery date populate the "delivery_date" box, I'd like it to pass through a calculation to show an order-by date in the "orderby_date" box. Is there a way to do this with update_post_meta?

Here is my code for the Delivery Date metabox:

<?php
function wpt_single_menu_deliverydate() {
    global $post;
    echo '<input type="hidden" name="singlemenumeta_noncename" id="singlemenumeta_noncename" value="' .
    wp_create_nonce( plugin_basename(__FILE__) ) . '" />';
    $deliver = get_post_meta($post->ID, '_deliverydate', true);
    echo '<input type="datetime-local" name="_deliverydate" value="' . $deliver  . '" class="widefat" />';
} ?>

and here is my code to output the two custom meta boxes based on that data (in my save meta function):

<?php
if(get_post_meta($post->ID, $key, FALSE)) { 
    update_post_meta($post->ID, $key, $value);
    update_post_meta( $post_id, 'delivery_date', stripslashes( $_POST['_deliverydate'] ) );
    update_post_meta( $post_id, 'orderby_date', stripslashes( $_POST['orderby_date'] ) );
?>

My question is how can I combine

date('Y-m-d', strtotime('-1 week last Sunday'));

into this so my Order-By date is the Sunday prior to the Delivery Date?

Upvotes: 0

Views: 134

Answers (1)

aleks1217
aleks1217

Reputation: 87

Admittedly, I am a noobie and cobbling together code from snippets that seem relevant. My solution was to add a second 'update_post_meta' and add date(strtotime()) to return the values into the custom fields I created. Then I created an argument for the instance, so I could modify the date for the second instance.

This worked:

if(get_post_meta($post->ID, $key, FALSE)) { 
    update_post_meta( $post_id, 'delivery_date', stripslashes( $_POST['delivery_date'] ) );
    update_post_meta( $post_id, 'delivery_date', date("Y-m-d", strtotime( $_POST[('_deliverydate')] )) );
$delivery = date("Y-m-d", strtotime( $_POST[('_deliverydate')] ));
    update_post_meta( $post_id, 'orderby_date', stripslashes( $_POST['orderby_date'] ) );
    update_post_meta( $post_id, 'orderby_date', date("Y-m-d", strtotime( "$delivery - 1 week last Sunday" )) );

Thus, after entering a date in my new Delivery Date meta box and updating my post, two custom fields are created. One returns the value of the Delivery Date meta box, and other converts to the Sunday before last to return my Order-By Date.

Upvotes: 0

Related Questions