Skip to main content
deleted 13 characters in body
Source Link
Nate Allen
  • 2.1k
  • 2
  • 16
  • 23

Normally you would write a function that gets called by the save_post hook, and use wp_update_post to update the post. The problem is, when you call wp_update_post it calls save_post, which will cause an infinite loop.

To get around that, we will unhook our function before calling wp_update_post, then re-hook it afterward.

I have not tried this, but it should work:

function change_post_date( $post_id, $post ) {

    // WordPress calls "save_post" when a post is saved, updated, autosaved,
    // revision created, or ajax called. We only want to execute this function
    // during post save and update. The next 3 "if" statements check to see why
    // save_post was called...

    // Autosave? Do nothing
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
        return;
    // AJAX? Do nothing
    if ( defined( 'DOING_AJAX' ) && DOING_AJAX )
        return;
    // Post revision? Do nothing
    if ( false !== wp_is_post_revision( $post_id ) )
        return;

    // Make sure the person editing the post has permission to do so
    if ( ! current_user_can( 'edit_post', $post_id ) )
        return;

    // Get the "date-time" field
    $date_time_field = get_post_meta($post_id, 'date-time', true);

    // Unhook
    remove_action('save_post', 'change_post_date', 10);

    $args = array (
        'ID' => $post_id,
        'post_date' => $date_time_field,
        'post_date_gmt' => gmdate('Y-m-d H:i:s'i', strtotime($date_time_field) )
    );

    wp_update_post( $args );

    // Re-hook
    add_action('save_post', 'change_post_date', 10);

}
add_filter('save_post', 'change_post_date', 10, 2);

Normally you would write a function that gets called by the save_post hook, and use wp_update_post to update the post. The problem is, when you call wp_update_post it calls save_post, which will cause an infinite loop.

To get around that, we will unhook our function before calling wp_update_post, then re-hook it afterward.

I have not tried this, but it should work:

function change_post_date( $post_id, $post ) {

    // WordPress calls "save_post" when a post is saved, updated, autosaved,
    // revision created, or ajax called. We only want to execute this function
    // during post save and update. The next 3 "if" statements check to see why
    // save_post was called...

    // Autosave? Do nothing
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
        return;
    // AJAX? Do nothing
    if ( defined( 'DOING_AJAX' ) && DOING_AJAX )
        return;
    // Post revision? Do nothing
    if ( false !== wp_is_post_revision( $post_id ) )
        return;

    // Make sure the person editing the post has permission to do so
    if ( ! current_user_can( 'edit_post', $post_id ) )
        return;

    // Get the "date-time" field
    $date_time_field = get_post_meta($post_id, 'date-time', true);

    // Unhook
    remove_action('save_post', 'change_post_date', 10);

    $args = array (
        'ID' => $post_id,
        'post_date' => $date_time_field,
        'post_date_gmt' => gmdate('Y-m-d H:i:s', strtotime($date_time_field) )
    );

    wp_update_post( $args );

    // Re-hook
    add_action('save_post', 'change_post_date', 10);

}
add_filter('save_post', 'change_post_date', 10, 2);

Normally you would write a function that gets called by the save_post hook, and use wp_update_post to update the post. The problem is, when you call wp_update_post it calls save_post, which will cause an infinite loop.

To get around that, we will unhook our function before calling wp_update_post, then re-hook it afterward.

I have not tried this, but it should work:

function change_post_date( $post_id, $post ) {

    // WordPress calls "save_post" when a post is saved, updated, autosaved,
    // revision created, or ajax called. We only want to execute this function
    // during post save and update. The next 3 "if" statements check to see why
    // save_post was called...

    // Autosave? Do nothing
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
        return;
    // AJAX? Do nothing
    if ( defined( 'DOING_AJAX' ) && DOING_AJAX )
        return;
    // Post revision? Do nothing
    if ( false !== wp_is_post_revision( $post_id ) )
        return;

    // Make sure the person editing the post has permission to do so
    if ( ! current_user_can( 'edit_post', $post_id ) )
        return;

    // Get the "date-time" field
    $date_time_field = get_post_meta($post_id, 'date-time', true);

    // Unhook
    remove_action('save_post', 'change_post_date', 10);

    $args = array (
        'ID' => $post_id,
        'post_date' => $date_time_field,
        'post_date_gmt' => gmdate('Y-m-d H:i', $date_time_field )
    );

    wp_update_post( $args );

    // Re-hook
    add_action('save_post', 'change_post_date', 10);

}
add_filter('save_post', 'change_post_date', 10, 2);
Changed code so it removes the hook and adds it back in, which is a better method
Source Link
Nate Allen
  • 2.1k
  • 2
  • 16
  • 23

Normally you would write a function that gets called by the save_post hook, and use wp_update_post to update the post. The problem is, when you call wp_update_post it calls save_post, which will cause an infinite loop.

To get around that, we will check if the post date is the same as the date in the custom field. If it is, we won't update the postunhook our function before calling (and avoid an infinite loop)wp_update_post, then re-hook it afterward.

I have not tried this, but it should work:

function change_post_date( $post_id, $post ) {

    // WordPress calls "save_post" when a post is saved, updated, autosaved,
    // revision created, or ajax called. We only want to execute this function
    // during post save and update. The next 3 "if" statements check to see why
    // save_post was called...

    // Autosave? Do nothing
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
        return;
    // AJAX? Do nothing
    if ( defined( 'DOING_AJAX' ) && DOING_AJAX )
        return;
    // Post revision? Do nothing
    if ( false !== wp_is_post_revision( $post_id ) )
        return;

    // Make sure the person editing the post has permission to do so
    if ( ! current_user_can( 'edit_post', $post_id ) )
        return;

    // Get the "date-time" field
    $date_time_field = get_post_meta($post_id, 'date-time', true);

    // If the post date is the same as the "date-time" field, do nothingUnhook
    if remove_action( $date_time_field =='save_post', $post->post_date'change_post_date', 10)
        return;;

    $args = array (
        'ID' => $post_id,
        'post_date' => $date_time_field,
        'post_date_gmt' => gmdate('Y-m-d H:i:s', strtotime($date_time_field) )
    );

    wp_update_post( $args );

    // Re-hook
    add_action('save_post', 'change_post_date', 10);

}
add_filter('save_post', 'change_post_date', 10, 2);

Normally you would write a function that gets called by the save_post hook, and use wp_update_post to update the post. The problem is, when you call wp_update_post it calls save_post, which will cause an infinite loop.

To get around that, we will check if the post date is the same as the date in the custom field. If it is, we won't update the post (and avoid an infinite loop).

I have not tried this, but it should work:

function change_post_date( $post_id, $post ) {

    // WordPress calls "save_post" when a post is saved, updated, autosaved,
    // revision created, or ajax called. We only want to execute this function
    // during post save and update. The next 3 "if" statements check to see why
    // save_post was called...

    // Autosave? Do nothing
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
        return;
    // AJAX? Do nothing
    if ( defined( 'DOING_AJAX' ) && DOING_AJAX )
        return;
    // Post revision? Do nothing
    if ( false !== wp_is_post_revision( $post_id ) )
        return;

    // Make sure the person editing the post has permission to do so
    if ( ! current_user_can( 'edit_post', $post_id ) )
        return;

    // Get the "date-time" field
    $date_time_field = get_post_meta($post_id, 'date-time', true);

    // If the post date is the same as the "date-time" field, do nothing
    if ( $date_time_field == $post->post_date )
        return;

    $args = array (
        'ID' => $post_id,
        'post_date' => $date_time_field,
        'post_date_gmt' => gmdate('Y-m-d H:i:s', strtotime($date_time_field) )
    );

    wp_update_post( $args );

}
add_filter('save_post', 'change_post_date', 10, 2);

Normally you would write a function that gets called by the save_post hook, and use wp_update_post to update the post. The problem is, when you call wp_update_post it calls save_post, which will cause an infinite loop.

To get around that, we will unhook our function before calling wp_update_post, then re-hook it afterward.

I have not tried this, but it should work:

function change_post_date( $post_id, $post ) {

    // WordPress calls "save_post" when a post is saved, updated, autosaved,
    // revision created, or ajax called. We only want to execute this function
    // during post save and update. The next 3 "if" statements check to see why
    // save_post was called...

    // Autosave? Do nothing
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
        return;
    // AJAX? Do nothing
    if ( defined( 'DOING_AJAX' ) && DOING_AJAX )
        return;
    // Post revision? Do nothing
    if ( false !== wp_is_post_revision( $post_id ) )
        return;

    // Make sure the person editing the post has permission to do so
    if ( ! current_user_can( 'edit_post', $post_id ) )
        return;

    // Get the "date-time" field
    $date_time_field = get_post_meta($post_id, 'date-time', true);

    // Unhook
    remove_action('save_post', 'change_post_date', 10);

    $args = array (
        'ID' => $post_id,
        'post_date' => $date_time_field,
        'post_date_gmt' => gmdate('Y-m-d H:i:s', strtotime($date_time_field) )
    );

    wp_update_post( $args );

    // Re-hook
    add_action('save_post', 'change_post_date', 10);

}
add_filter('save_post', 'change_post_date', 10, 2);
Source Link
Nate Allen
  • 2.1k
  • 2
  • 16
  • 23

Normally you would write a function that gets called by the save_post hook, and use wp_update_post to update the post. The problem is, when you call wp_update_post it calls save_post, which will cause an infinite loop.

To get around that, we will check if the post date is the same as the date in the custom field. If it is, we won't update the post (and avoid an infinite loop).

I have not tried this, but it should work:

function change_post_date( $post_id, $post ) {

    // WordPress calls "save_post" when a post is saved, updated, autosaved,
    // revision created, or ajax called. We only want to execute this function
    // during post save and update. The next 3 "if" statements check to see why
    // save_post was called...

    // Autosave? Do nothing
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
        return;
    // AJAX? Do nothing
    if ( defined( 'DOING_AJAX' ) && DOING_AJAX )
        return;
    // Post revision? Do nothing
    if ( false !== wp_is_post_revision( $post_id ) )
        return;

    // Make sure the person editing the post has permission to do so
    if ( ! current_user_can( 'edit_post', $post_id ) )
        return;

    // Get the "date-time" field
    $date_time_field = get_post_meta($post_id, 'date-time', true);

    // If the post date is the same as the "date-time" field, do nothing
    if ( $date_time_field == $post->post_date )
        return;

    $args = array (
        'ID' => $post_id,
        'post_date' => $date_time_field,
        'post_date_gmt' => gmdate('Y-m-d H:i:s', strtotime($date_time_field) )
    );

    wp_update_post( $args );

}
add_filter('save_post', 'change_post_date', 10, 2);