Support

Account

Home Forums ACF PRO $_POST in new post title the first 3 characters of a field value

Solved

$_POST in new post title the first 3 characters of a field value

  • 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’re welcome… Can you also mark it solved ?

Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘$_POST in new post title the first 3 characters of a field value’ is closed to new replies.