Skip to content

Instantly share code, notes, and snippets.

@kontikidigital
Created January 17, 2023 17:22
Show Gist options
  • Save kontikidigital/e184aec1866dc9699aa9320a3ebee700 to your computer and use it in GitHub Desktop.
Save kontikidigital/e184aec1866dc9699aa9320a3ebee700 to your computer and use it in GitHub Desktop.
Generate post title and post slug from ACF single value fields
/**
* Generate post title and post slug from ACF single value fields
*
*/
add_action('acf/save_post', 'my_acf_save_post', 5);
function my_acf_save_post( $post_id ) {
$post_title = 'My default title';
$slug = 'my-default-slug';
// Check if a specific field value was updated.
// Replace field_key with the actual field keys of the fields
if( isset($_POST['acf']['field_key1']) && isset($_POST['acf']['field_key2']) && isset($_POST['acf']['field_key3']) && isset($_POST['acf']['field_key4']) && isset($_POST['acf']['field_key5'])) {
// Replace field_key with keys of the fields needed to generate title
$post_title = $_POST['acf']['field_key1'] . ' ' . $_POST['acf']['field_key2'] . ' ' . $_POST['acf']['field_key3'] . ' ' . $_POST['acf']['field_key4'] . ' ' . $_POST['acf']['field_key5'];
$slug = sanitize_title( $post_title );
}
// Grab Post Data from the Form
$post = array(
'ID' => $post_id,
'post_title' => $post_title,
'post_name' => $slug,
);
// Update the Post
wp_update_post( $post );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment