Support » Developing with WordPress » Post status transition with quick edit / bulk action

  • Resolved Sany

    (@sannny)


    Hello,

    I want to change the publish date when a post / post type is changed to a custom post status release.

    This is my code:

    function status_transition_test($new_status, $old_status, $post) {
    	
    	$time = current_time('mysql');
    	
    	if($old_status != 'release' && $new_status == 'release') {
    		
    		$data = array(
    			'post_status' => 'release',
    			'post_date' => $time,
    			'post_date_gmt' => get_gmt_from_date($time),
    		);
    		
    		wp_update_post($data);
    	}
    }
    add_action('transition_post_status', 'status_transition_test', 10, 3);

    This works fine when I open a post, change the status and save it.
    But if I change the status via quick edit / bulk action the date doesn’t change. I also tried to use the hook draft_to_release for example but this doesn’t work either.

    Does somebody have an advice, maybe I need to use another hook?

    Best regards

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    Transitions are tricky, especially if your update causes another transition. Try the ‘wp_insert_post_data’ filter which fires before the post is updated. You’ll need to get the current status from the DB since previous data is not passed to this filter, but otherwise I think it’ll work well for you.

    Thread Starter Sany

    (@sannny)

    Great, wp_insert_post_data filter is working well. Thank you very much.

    function custom_post_status_update_date($data, $postarr) {
    	
    	if($data['post_status'] == 'release' && get_post_status($postarr['ID']) != 'release') {
    		$time = current_time('mysql');
    		$data['post_date'] = $time;
    		$data['post_date_gmt'] = get_gmt_from_date($time);
    	}
    	
    	return $data;
    }
    add_filter('wp_insert_post_data', 'custom_post_status_update_date', 10, 2);
    • This reply was modified 2 years ago by Sany.
    • This reply was modified 2 years ago by Sany.
    Moderator bcworkz

    (@bcworkz)

    You’re welcome. It has become one of my favorite filters 🙂

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Post status transition with quick edit / bulk action’ is closed to new replies.