Piyush Mittal
Piyush Mittal

Reputation: 159

Wordpress update published date

when I update post published date from meta box field date it's not working. I am using save_post hook, but it is not working properly. Which function is used to update post date.

My code:

// Update publisged date from date of launch for concept

function update_concept_post_date( $post_id ){

    if ( ! wp_is_post_revision( $post_id ) ){       

        $post_data = get_post($post_id);
       $date = get_post_meta($post_id, '_cmb_concept_signup_date', true);
        $updated_date = explode('-', $date);
        $new_date   =   date("Y-m-d h:i:s", mktime(0,0,0,$updated_date[1],$updated_date[2],$updated_date[0]));
      if($post_data->post_type == 'concepts'){
          $my_post = array(
              'ID'           => $post_id,
              'post_date' => $new_date
          );
          // update the post, which calls save_post again
          wp_update_post( $my_post );
      }
    }
}
add_action('save_post', 'update_concept_post_date', 10);

Upvotes: 1

Views: 354

Answers (1)

Hüseyin BABAL
Hüseyin BABAL

Reputation: 15550

You are parsing date wrong, it should be;

$temp = explode(' ', $date);
$updated_date = explode('-', $temp[0]);

First parse it by space character, and first part becomes Y-m-d and then parse this part by -.

Upvotes: 1

Related Questions