admin管理员组文章数量:1024655
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
|
1 Answer
Reset to default -1Try 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.
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, thenstrtotime()
probably kicks back a null, and so you end up in a loop. Yoursave_post
routine withupdate_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
1 Answer
Reset to default -1Try 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.
本文标签: postssavepost not working
版权声明:本文标题:posts - save_post not working 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745611367a2159023.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
global $post;
Also, how is $offerstartdate originally defined? Because when retrieving the meta, if it is null, thenstrtotime()
probably kicks back a null, and so you end up in a loop. Yoursave_post
routine withupdate_post_meta
looks fine, so it is likely something else. – helgatheviking Commented Jan 30, 2014 at 13:17