Home › Forums › ACF PRO › $_POST in new post title the first 3 characters of a field value › Reply To: $_POST in new post title the first 3 characters of a field value
Maybe I’m missing it, but how can this function work ????
You change override/change $_POST but you don’t do anything with this var.
You only return the $post_id again.
For one of my sites I create post titles, based on an ACF field. It’s not exactly what you want, but it gives a good idea I think. See below:
function set_title_from_acf( $post_id ) {
// bail if no ACF data
if ( empty( $_POST[ 'acf' ] ) ) {
return;
}
$ad_title = 'field_57e3ed6c92ea0'; // input field
// if sd_ad_title is entered
if ( ! empty( $_POST[ 'acf' ][ $ad_title ] ) ) {
$post = get_post( $post_id );
$entered_title = $_POST[ 'acf' ][ $ad_title ];
$cleaned_title = preg_replace( '/[^A-Za-z0-9\-\s]/', '', $entered_title );
// Removes special chars.
$post_name = sanitize_title( $cleaned_title );
update_field( 'sd_ad_title', $cleaned_title, $post_id ); // update acf field
// update post slug + post title (if needed)
global $wpdb;
$wpdb->update(
$wpdb->posts,
array(
'post_title' => $cleaned_title,
'post_name' => $post_name
),
array(
'ID' => $post_id
)
);
clean_post_cache( $post_id );
}
}
add_action( 'acf/save_post', 'set_title_from_acf', 20 );
-=-
To get the first 3 characters from a certain field, substr is def. the way to go in my opinion.
$new_value = substr( $_POST['acf']['field_5a6b343038f0c'], 0, 3 );
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.