1

I recently added a function to reset the publish date when a user updates a page. The code is as follows, thanks to @s_ha_dum for it because it works great except for one annoying instance.

function reset_post_date_wpse_121565($data,$postarr) {
// var_dump($data,$postarr); die; // debug
$data['post_date'] = $data['post_modified'];
$data['post_date_gmt'] = $data['post_modified_gmt'];
return $data;}

add_filter('wp_insert_post_data','reset_post_date_wpse_121565',99,2);

I noticed it works as intended for posts and pages but I have a custom taxonomy where, when I update one item, in this case 'property' it updates the publish date for all of that same taxonomy. Any idea how to avoid this from happening?

Thanks!

  • Do you save taxonomy or custom post type? – Anton Lukin Aug 24 '17 at 11:53
1

You could also hook into the save_{$post_type} action, which fires only when a certain post type is updated. This hook passes to post's ID to the callback function:

add_action( 'save_post', 'my_callback_function' );
function my_callback_function( $post_id ){

    if ( ! wp_is_post_revision( $post_id ) ){

        // Unhook this function so it doesn't loop infinitely
        remove_action('save_post', 'my_callback_function');

        // Update post's data
          $post = array(
              'ID'              => $post_id,
              'post_date'       => current_time( 'mysql' ),
              'post_date_gmt'   => current_time( 'mysql', 1 ),
          );

        // Update the post, which calls save_post again
        wp_update_post( $post );

        // Re-hook this function
        add_action('save_post', 'my_callback_function');
    }

}
  • Thanks for the timely response! Unfortunately it is still updating the publishing date for all 'properties' – Patrik Aug 24 '17 at 16:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.