Change date when post expires

Closed
REHub - Price Comparison, Affiliate Marketing, Multi Vendor Store, Community Theme REHub - Price Comparison, Affiliate Marketing, Multi Vendor Store, Community Theme September 01, 2024
Login to reply
Bioinformatics India commented privately
Bioinformatics India
3 months ago

Love you so much :) 

Igor Sunz Support Agent
3 months ago
add_action('post_change_expired', 'cpt_expired_function', 11, 1);
function cpt_expired_function($expired=''){
global $post;
if (isset($post)){
   $post_id = $post->ID;
}else{
   return false;
}
$expired_exist = get_post_meta($post_id, 're_post_expired', true);
if($expired_exist == 1){
   $new_date = '2020-01-01 00:00:00';
   $postarr = array(
      'ID' => $post_id,
      'post_date' => $new_date,
      'post_date_gmt' => get_gmt_from_date($new_date)
   );
   wp_update_post($postarr);
}
}
Bioinformatics India
3 months ago

Support renewed to get this one resolved, Please update.

Bioinformatics India
3 months ago

Hi Igor, Can you help me with a code where when the coupon code expires, the date of that post is automatically set to some date in 2019?

I tried the below code, but it is not working

add_action('post_change_expired', 'post_change_expired_function', 10, 1);

if (!function_exists('post_change_expired_function')) {

    function post_change_expired_function($expired=''){

        global $post;

        if (isset($post)){

            $post_id = $post->ID;

        }

        else{

            return false; // Error: no POST ID

        }

        $expired_exist = get_post_meta($post_id, 're_post_expired', true);

        if($expired == 1 && $expired_exist != 1){

            update_post_meta($post_id, 're_post_expired', 1);

            wp_set_object_terms($post_id, 'yes', 'offerexpiration', false );

            // Set the post date to January 1, 2020

            $new_date = '2020-01-01 00:00:00';

            $postarr = array(

                'ID' => $post_id,

                'post_date' => $new_date,

                'post_date_gmt' => get_gmt_from_date($new_date)

            );

            wp_update_post($postarr);

        } elseif($expired == 'no'){

            update_post_meta($post_id, 're_post_expired', 0);

            wp_set_object_terms($post_id, NULL, 'offerexpiration', false );

        } elseif($expired_exist == 0){

            // No action needed if not expired

        } elseif($expired_exist == ''){

            update_post_meta($post_id, 're_post_expired', 0);

            wp_set_object_terms($post_id, NULL, 'offerexpiration', false );

        }

    }

}

Additional Info

Link on page with issue: https://couponzania.com/