Support

Account

Home Forums General Issues Automatic post title from custom fields

Solved

Automatic post title from custom fields

  • Hi Guys,

    I would like to automatically create the title of a POST from two Custom Fields when creating a new POST. The fields are populated via acf/load_value

    EG:

    <input type=”text” name=”firstname” id=”firstname” value=”Fred”>
    <input type=”text” name=”lastname” id=”lastname” value=”Bloggs”>

    The title of the new page would be: Fred Bloggs

    Thanks for you help, Ian.

  • OK, so this works on UPDATE but not on PUBLISH when the initial post is created:

    function my_function( $value ) {
        if ( empty( $value ) ) :
            return get_field( "firstname" ) .  get_field( "lastname" );
        endif;
        return $value;
    }
    add_filter('title_save_pre', 'my_function');

    Any ideas anyone?

  • Hi @Ian

    That is because your filter runs BEFORE ACF has saved any postmeta data.

    Instead of using get_field( "firstname" ), you will need to find the value from the $_POST array. Please note that all data is posted via it’s field key, not field name.

    You can learn more about this in the docs.

    Thanks
    E

  • Thanks Elliot, that make a lot of sense… I’m new to backend development so I was wondering if you could point me in the right direction for using the $_POST array and field key?

    I’ve tried the following code without success:

    function my_function( $value ) {
        if ( empty( $value ) ) :
            return $_POST['field_5252ea93ea1f6'] .  $_POST['field_5252aeaf0a86b'];
        endif;
        return $value;
    }
    add_filter('title_save_pre', 'my_function');

    Am I going off in the completely wrong direction?

    Thanks, Ian.

  • Hi Elliot, I have the answer:

    function my_function( $value ) {
        if ( empty( $value ) ) :
    	$value = $_POST['fields']['field_5252ea93ea1f6'] . $_POST['fields']['field_5252aeaf0a86b'];
            return $value;
        endif;
        return $value;
    }
    add_filter('title_save_pre', 'my_function');

    Sorry for the Newbie questions and thanks for your help.

    Regards, Ian.

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

The topic ‘Automatic post title from custom fields’ is closed to new replies.