Advertisement
pjeaje

Set the_date() according to custom field = yyyy-mm-dd 12:45

Sep 24th, 2015
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.26 KB | None | 0 0
  1. function change_post_date( $post_id, $post ) {
  2.  
  3.     // WordPress calls "save_post" when a post is saved, updated, autosaved,
  4.     // revision created, or ajax called. We only want to execute this function
  5.     // during post save and update. The next 3 "if" statements check to see why
  6.     // save_post was called...
  7.  
  8.     // Autosave? Do nothing
  9.     if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
  10.         return;
  11.     // AJAX? Do nothing
  12.     if ( defined( 'DOING_AJAX' ) && DOING_AJAX )
  13.         return;
  14.     // Post revision? Do nothing
  15.     if ( false !== wp_is_post_revision( $post_id ) )
  16.         return;
  17.  
  18.     // Make sure the person editing the post has permission to do so
  19.     if ( ! current_user_can( 'edit_post', $post_id ) )
  20.         return;
  21.  
  22.     // Get the "date-time" field
  23.     $date_time_field = get_post_meta($post_id, 'date-time', true);
  24.  
  25.     // Unhook
  26.     remove_action('save_post', 'change_post_date', 10);
  27.  
  28.     $args = array (
  29.         'ID' => $post_id,
  30.         'post_date' => $date_time_field,
  31.         'post_date_gmt' => gmdate('Y-m-d H:i:s', strtotime($date_time_field) )
  32.     );
  33.  
  34.     wp_update_post( $args );
  35.  
  36.     // Re-hook
  37.     add_action('save_post', 'change_post_date', 10);
  38.  
  39. }
  40. add_filter('save_post', 'change_post_date', 10, 2);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement