1

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 );
}
  • 1
    With publish_post, you can use $post->post_date instead of get_the_date(). So try with strtotime( '+2 weeks', strtotime( $post->post_date ) ). I.e. Get the post publish date timestamp, and use it with your existing strtotime() call. – Sally CJ Oct 3 '18 at 18:59
  • Hi, thanks for your response. I updated my original post with code that I tried, but it didn't do anything. – Edegist Oct 3 '18 at 19:48
0

Try this:

add_action('publish_post', 'dp_expiry');
function dp_expiry( $data ) {
    /*adds 2 weeks onto the current date in unix epoch format*/
$dp_new_expiry_date = (strtotime( current_time( 'mysql' ) ) + 1209600);
        /*converts unix timstamp back to yyyy-mm-dd format like you required*/
    $dp_new_expiry_date_conv = date("Y-m-d", $dp_new_expiry_date);
update_post_meta( $data['post_id'], 'postexpiry', $dp_new_expiry_date_conv );
}
  • This is not canonical, but some sort of individual hack, and should be marked as such, in my opinion: developer.wordpress.org/reference/hooks/…. Otherwise, people who read this will expect their publish_post hook to be passed a $data array type, which it doesn't. – Michael Oct 4 '18 at 19:10
0

get_the_ID() is a loop function and might not work here. You should use the parameters of your callback instead:

function dp_expiry( $ID, $post ) {

    $dp_new_expiry_date = strtotime( '+2 weeks', strtotime( $post->post_date ) );

    update_post_meta( $ID, 'postexpiry', $dp_new_expiry_date );
}

add_action( 'publish_post', 'dp_expiry', 10, 2 );

publish_post only works for posts, not pages. You can use publish_{post_type} for a custom post type.

  • Thanks. Tried it, but still nothing. I just noticed my them requires that the date be in yyyy-mm-dd format. Could that be the cause of the problem? – Edegist Oct 3 '18 at 20:55
  • What do you mean by nothing? It works on a clean WP install, and it only works if the post status changes to 'publish'. – Michael Oct 3 '18 at 20:57
  • I wasn't getting anything in the meta field with that code. When I echoed the field, it just said the word "array". I changed around the code a little (posted above), and now it saves "1209600" to the field. Any ideas? Thanks! – Edegist Oct 4 '18 at 6:05
  • Sorry, I have no idea what you are passing to your dp_expiry function. Myself, I am using Wordpress and the publish_post hook: codex.wordpress.org/Plugin_API/Action_Reference/publish_post. Have a nice day. – Michael Oct 4 '18 at 11:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.