Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save asharirfan/25a288da36fa66c609c4f5fe04cadb3c to your computer and use it in GitHub Desktop.
Save asharirfan/25a288da36fa66c609c4f5fe04cadb3c to your computer and use it in GitHub Desktop.
Detect if WordPress post is published for the first time
<?php
/*
* On first publish set a special date in advance in custom field
*/
add_action('transition_post_status','tanc_first_schedule_publish_set',10,3);
function tanc_first_schedule_publish_set($new, $old, $post) {
// REFERENCE
// $new = new post status ('publish')
// $old = old post status ('draft')
// $post = Post Object ($post->ID)
// On first publish
if ($new == 'publish' && $old != 'publish' && isset($post->post_type)) {
switch ($post->post_type) {
case 'type_one' :
update_post_meta( $post->ID, 'type_one_expiry_date', strftime( '%Y-%m-%d', (strftime('%Y-%m-%d', time()) + strtotime('+365 days')) ) );
break;
case 'type_two' :
update_post_meta( $post->ID, 'type_one_expiry_date', strftime( '%Y-%m-%d', (strftime('%Y-%m-%d', time()) + strtotime('+31 days')) ) );
break;
default:
// Silence is golden
break;
}
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment