-
-
Save asharirfan/25a288da36fa66c609c4f5fe04cadb3c to your computer and use it in GitHub Desktop.
Detect if WordPress post is published for the first time
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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