I have a meta field called postexpiry
, and I want to set the value to the publish date, + 2 weeks.
So if today were October 3rd, I want the field to be set to October 17th.
I was thinking about creating a hook to the publish_post
filter, but I am not sure how to add 2 weeks on to get_the_date()
.
I know with php I can do something like this $dateInTwoWeeks = strtotime('+2 weeks');
but I'm not sure how to use that with get_the_date()
Thanks!
UPDATE
I tried the following code, but it didn't do anything:
function dp_expiry() {
$dp_new_expiry_date = strtotime( '+2 weeks', strtotime( $post->post_date ) );
update_post_meta( get_the_ID(), 'postexpiry', $dp_new_expiry_date );
}
add_action( 'publish_post', 'dp_expiry' );
Also, my theme requires that the date be in yyyy-mm-dd
format.
UPDATE 2
This code outputs "1209600" to the field. Any ideas? Thanks!
add_action('publish_post', 'dp_expiry');
function dp_expiry( $data ) {
$dp_new_expiry_date = strtotime( '+2 weeks', strtotime( $post->post_date ) );
update_post_meta( $data['post_id'], 'postexpiry', $dp_new_expiry_date );
}
publish_post
, you can use$post->post_date
instead ofget_the_date()
. So try withstrtotime( '+2 weeks', strtotime( $post->post_date ) )
. I.e. Get the post publish date timestamp, and use it with your existingstrtotime()
call.