1

I am trying to create/update post meta when you create/update a post that is specifically a post type of "offer". However, it does not update the post meta. This code is added in my functions.php file.

add_filter( 'pre_post_update', 'update_voucher_deadline', 10, 2 );
function update_voucher_deadline( $post_id, $data ) { 
    $evergreen = get_field('offer_evergreen', $post_id);
    if ($evergreen == "evergreen-yes") {
        $year = date('Y');
        $month = date('m');
        $currentDate = "". $year . "-" . $month . "-" . date('d') . date('H') . ":" . date('i') . ":" . date('s');
        $day = date("t", strtotime($currentDate));
        $endOfMonth = "". $year . "-" . $month . "-" . $day . "23:59:00";

        //global $post; Tried with this uncommented and also didn't work.

        if ( ! add_post_meta($post_id, 'offer_voucher_deadline', $endOfMonth)) { 
            update_post_meta($post_id, 'offer_voucher_deadline', $endOfMonth);
        }
    }
}
Daniel Vickers
  • 1,054
  • 1
  • 12
  • 32
  • The "Warning: Missing argument 3 for update_promo_tag_id()" appears because your function asks for 3 arguments, but the action only provides 2 when called by wordpress -- i.e: do_action( 'pre_post_update', int $post_ID, array $data ) – Design.Garden Oct 17 '18 at 21:40
  • The "Cannot modify header information - headers already sent" is probably from something else. You get this error when you attempt to set header info after PHP has already started building the page content – Design.Garden Oct 17 '18 at 21:41
  • The update_post_meta function fails because your trying to get the post id from the array of post data passed into your function ($post is not the post, it is the post data). Use the $post_id instead – Design.Garden Oct 17 '18 at 21:43
  • Updated answer, not sure what you mean with the $post_id thing what am I replacing. Need some clarity so I understand whats changing and why. Can you assist? – Daniel Vickers Oct 17 '18 at 21:45
  • UPDATE: just saw that you are using the "global $post" -- this code is a little bad because global variables must be declared at the start of the function. Use $post_id instead – Design.Garden Oct 17 '18 at 21:46
  • Change final if statement to the following, and get rid of the "global $post" line: if ( ! add_post_meta($post_id, 'offer_voucher_deadline', $endOfMonth)) { update_post_meta($post_id, 'offer_voucher_deadline', $endOfMonth); } – Design.Garden Oct 17 '18 at 21:46
  • Changed it to that and it's still not updating, also updated question to detail new code. – Daniel Vickers Oct 17 '18 at 21:48
  • This won't fix it, but update the following: add_filter( 'pre_post_update', 'update_voucher_deadline', 10, 2 ); – Design.Garden Oct 17 '18 at 21:49
  • Okay, everything looks good to me, so my bet is that the get_field function is not working as expected. Can you point me to documentation on this function? Your use of it looks a little questionable – Design.Garden Oct 17 '18 at 21:53
  • Guessing it should be: $evergreen = get_field('offer_evergreen', $post_id); – Design.Garden Oct 17 '18 at 21:54
  • It relates to advanced custom fields, I have also had this thought that it doesn't know the post id to get it from, tried adding $post_id to cover that and still didn't work. https://www.advancedcustomfields.com/resources/get_field/ – Daniel Vickers Oct 17 '18 at 21:54
  • Code looks good now... my guess is that if ($evergreen == "evergreen-yes") evaluates to false. Do a some debugging on your offer_evergreen field – Design.Garden Oct 17 '18 at 22:00

2 Answers2

3

I see you use ACF plugin to create custom field which means you can use acf/save_post filter to do this something like this.
1. Check if we save post type 'offer'
2. Check if we have custom field 'offer_evergreen' with value 'evergreen-yes'
3. Check if we have custom filed 'offer_voucher_deadline' if yes - update him.
4. If we do not have custom filed 'offer_voucher_deadline' create him and save our data.

add_filter('acf/save_post', 'update_voucher_deadline', 20);
function update_voucher_deadline($post_id) {
    if ( get_post_type($post_id) != 'offer' ) //if current post type not equal 'offer' return
        return;

         $year = date('Y');
         $month = date('m');
         $currentDate = "". $year . "-" . $month . "-" . date('d') . date('H') . ":" . date('i') . ":" . date('s');
         $day = date("t", strtotime($currentDate));
         $endOfMonth = "". $year . "-" . $month . "-" . $day . "23:59:00";

    if ( get_field('offer_evergreen') == 'evergreen-yes' ) {

        if ( get_post_meta( $post_id, 'offer_voucher_deadline', true ) ) //If get post meta with key 'offer_voucher_deadline' - update meta
            update_post_meta($post_id, 'offer_voucher_deadline', $endOfMonth);
        else //else if do not have post meta with key 'offer_voucher_deadline' create post meta
            add_post_meta( $post_id, 'offer_voucher_deadline', $endOfMonth);

    } else {
        return; //Remove return and add what you want to save, if offer_evergreen not equal to evergreen-yes
    }
}
  • You are amazing, I forgot about the acf hook as I have never had to use it before. Much appreciated! – Daniel Vickers Oct 17 '18 at 22:02
  • Now when evergreen is not == yes it does not save the value as the regular field of offer_voucher_deadline – Daniel Vickers Oct 17 '18 at 22:05
  • You can replace the second return after else with what you want to save, if evergreen is not == yes – Кирилл Меркушев Oct 17 '18 at 22:09
  • Im too tired for this, the deadline field had updated so even though I added the correct code in the else statement it was always going to show the same as the previously set deadline... thanks for the help! – Daniel Vickers Oct 17 '18 at 22:11
  • you can short code as well not need to check get_post_meta() exist or not just use both time update_post_meta() options / auto created if not exits value. – Ravi Patel Oct 18 '18 at 09:26
1

First of all pre_post_update hook will not fires on create, it will fires immediately before an existing post is updated.

You need to use save_post hook it will triggered whenever a post or page is created or updated

add_action( 'save_post', 'update_voucher_deadline', 10, 3 );
/**
 * Save post metadata when a post is saved.
 *
 * @param int $post_id The post ID.
 * @param post $post The post object.
 * @param bool $update Whether this is an existing post being updated or not.
 */
function update_voucher_deadline( $post_id, $post, $update ) { 
    $evergreen = get_field('offer_evergreen');
    if ($evergreen == "evergreen-yes") {
        $year = date('Y');
        $month = date('m');
        $currentDate = "". $year . "-" . $month . "-" . date('d') . date('H') . ":" . date('i') . ":" . date('s');
        $day = date("t", strtotime($currentDate));
        $endOfMonth = "". $year . "-" . $month . "-" . $day . "23:59:00";

        //global $post; Tried with this uncommented and also didn't work.

        if ( ! add_post_meta($post_id, 'offer_voucher_deadline', $endOfMonth)) { 
            update_post_meta($post_id, 'offer_voucher_deadline', $endOfMonth);
        }
    }
}

Note: you can update your function using $update param if necessary.