Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

In order to create (or update, whenever a post is updated) a unix-timestamp meta, I used following code to fire on save_post. I pasted this code in functions.php.

But I believe this is not working, as I can't access the related unix meta created by save_post in loop anywhere in a custom template.

EDIT: After answers and comments I added priority and arguments in save_post hook event and also updated the way of retrieval. The problem now seems as follows - When a post is created then the save_post does not fires, but if I update the post (after creating it) save-post fires.

Plus, on updation (at any point after creation) if I change values of start date and end date, it doesn't reflect, it always takes the value which was used while creating the post.

Code -

function vg_update_timestamp( $post_id, $post ) {

    $offerstartdate = get_post_meta($post_id, 'offer_cmb_offer_from_textdate', true); //not unix timestamp
    $offerenddate =  get_post_meta($post_id, 'offer_cmb_offer_till_textdate', true);  //not unix timestamp

    $timestamp_start = strtotime( $offerstartdate );
    $timestamp_end = strtotime( $offerenddate );

    update_post_meta($post_id, 'offer_cmb_offer_from_textdate_unix', $timestamp_start );    
    update_post_meta($post_id, 'offer_cmb_offer_till_textdate_unix', $timestamp_end );  
}
add_action( 'save_post', 'vg_update_timestamp', 0, 2 );

In Template (Custom Template) am using following code to retrieve the field -

$unix_version_s = get_post_meta($post->ID, 'offer_cmb_offer_from_textdate_unix', true);
$unix_version_e = get_post_meta($post->ID, 'offer_cmb_offer_till_textdate_unix', true);

$unix_start = strtotime($unix_version_s);
$unix_end = strtotime($unix_version_e);

/******
Converting date to desired format 
**************************************/

$offerstartdate = gmdate("l, M j, Y", $unix_start);
$offerenddate = gmdate("l, M j, Y", $unix_end);

/*****
and then am using $offerstartdate and $offerenddate wherever needed, but its returning Jan 1, 1970 [which i think is because the value is zero or nill]
*****/
share|improve this question
    
How are you retrieving the data in a template? Can you show us the code? Are you sure the variables are not being updated? –  sri Jan 30 at 12:43
    
@sri I added the code am using for retrieval in the question. –  vajrasar Jan 30 at 12:49
    
In your template, are you globally declaring $post? global $post; Also, how is $offerstartdate originally defined? Because when retrieving the meta, if it is null, then strtotime() probably kicks back a null, and so you end up in a loop. Your save_post routine with update_post_meta looks fine, so it is likely something else. –  helgatheviking Jan 30 at 13:17
    
I have edited and updated the details. Please consider that. –  vajrasar Jan 30 at 13:36

1 Answer 1

Try this,

add_action( 'save_post', 'vg_update_timestamp', 0, 1 );

May be this will help...

I think i made a little mistake...But i am little confused about priorities...

so try this add_action( 'save_post', 'vg_update_timestamp', 10, 2 );.

Because, Docs says Lower numbers correspond with earlier execution, and functions with the same priority are executed in the order in which they were added to the action.

share|improve this answer
    
Can you elaborate on why this would help? Perhaps some information on the save_post hook and why the extra parameters make a difference? –  Tom J Nowell Jan 30 at 13:04
    
I think Forth parameter is used for accepting number of argument, In your function you use $post_id so may be this will help... –  Akshay PãghdÂr Jan 30 at 13:09
    
Yes. 0 is for priority and 1 is for argument. Anyways, i did that but that too din't help. Plus, using $post_id as $post -> ID won't be necessary, as we need not call the object. –  vajrasar Jan 30 at 13:23
    
I have found some code, Try this... add_action( 'save_post', 'vg_update_timestamp', 0, 2 ); function vg_update_timestamp( $post_id, $post ){ //Your Code } –  Akshay PãghdÂr Jan 30 at 13:30
1  
@AkshayPaghdar please do not offer random code in comments. If you have an answer, you can always update your answer(if you have already answered) or add a new answer. –  sri Jan 30 at 13:40

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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