最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

posts - save_post not working

programmeradmin2浏览0评论

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]
*****/

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 edited Jan 30, 2014 at 13:52 vajrasar asked Jan 30, 2014 at 12:36 vajrasarvajrasar 1693 silver badges19 bronze badges 4
  • 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 Commented Jan 30, 2014 at 12:43
  • @sri I added the code am using for retrieval in the question. – vajrasar Commented Jan 30, 2014 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 Commented Jan 30, 2014 at 13:17
  • I have edited and updated the details. Please consider that. – vajrasar Commented Jan 30, 2014 at 13:36
Add a comment  | 

1 Answer 1

Reset to default -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.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论