Hello,
i have the following function setup to populate a new posts title based on the values of four fields:
function acf_review_before_save_post($post_id) {
if (empty($_POST[‘acf’]))
return;
$_POST[‘acf’][‘_post_title’] = $_POST[‘acf’][‘field_5a6b343038f0c’].’ ‘.$_POST[‘acf’][‘field_5a6770a3c509d’].’ ‘.$_POST[‘acf’][‘field_5a5f76d271881′].’ ‘.$_POST[‘acf’][‘field_5a6b3459d911d’];
return $post_id;
}
I want to get only the first 3 chars of one of the fields
i have tried this substr($_POST[‘acf’][‘field_5a6b343038f0c’],1) as well as this $_POST[‘acf’][‘field_5a6b343038f0c’][0] but whenever i use one of these two methods the post creation doesn’t go through
Any suggestion on how i can just get the first 3 chars of a value in a function of this type?
PS: the function works properly otherwise
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 );
This did the trick for me!
basically i used this to get the first 3 characters: mb_strimwidth($_POST['acf']['field_5a6b343038f0c'], 0, 3 )
Below you can see the function:
function acf_review_before_save_post($post_id) {
if (empty($_POST['acf']))
return;
$_POST['acf']['_post_title'] =$_POST['acf']['field_5a6770a3c509d'].' '.mb_strimwidth($_POST['acf']['field_5a6b343038f0c'], 0, 3 ).' '.$_POST['acf']['field_5a5f76d271881'].' '.date("d/m/Y", strtotime($_POST['acf']['field_5a6b3543bf97a']));
return $post_id;
}
add_action('acf/pre_save_post', 'acf_review_before_save_post', -1);
Thanks for your response!
You must be logged in to reply to this topic.
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!
🚨 The 2023 ACF Annual Survey closes tomorrow! This is your last chance to complete the survey and help guide the evolution of ACF. https://t.co/0cgr9ZFOJ5
— Advanced Custom Fields (@wp_acf) May 18, 2023
© 2023 Advanced Custom Fields.
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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.