A
A
Alexander Sobolev2021-08-09 19:53:38
WordPress
Alexander Sobolev, 2021-08-09 19:53:38

How to rename a Wordpress post before publishing?

In order to avoid problems with random names like "111, 12345, qwerty" etc, I'm trying to write a function that intercepts the custom type post when publishing and renames the post according to the data specified in the custom fields of the post..
Something goes wrong..

add_action( 'wp_insert_post_data', 'es_eventRenamer', 20, 2 );

function es_eventRenamer( $data, $postarr ){
  global $post;
  if( ( empty( $_POST['post_type'] ) || 'dk_event' !== $_POST['post_type'] )
      || ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
      || ( 'dk_event' !== get_current_screen()->id )
      || ! current_user_can( 'edit_post', $postarr['ID'] )
  )   return $data;

  $data['post_title'] = $data["meta_input"]["event_type"] .' : '. $data["meta_input"]["start_date"].' в '.$data["meta_input"]["start_time"];

  return $data;
}

UPD:
I tried to make an update option with wp_after_insert_post hook
add_action( 'wp_after_insert_post', 'es_eventRenamer', 10, 4 );
function es_eventRenamer( $post_id, $post, $update, $post_before ){

  $event_type = get_field( 'event_type', $post_id);
  $start_date = get_field( 'start_date', $post_id);
  $start_time = get_field( 'start_time', $post_id);
  
  if($post->post_type == "dk_events"){
    $event = array();
    $event['ID'] = $post_id;
    $event['post_title'] = $event_type->name.' '.$start_date.' в '.$start_time ;
    wp_update_post( wp_slash($event) );

  }
}

post_title fixes, but gives a fatality Maximum execution time of 30 seconds exceeded
How to fix it?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question