Support

Account

Home Forums Backend Issues (wp-admin) Use post title as default value

Solved

Use post title as default value

  • Hi,

    is there a posibility to put the post title automaticly as the default value in a normal acf textfield? Maybe on load of the page in backend?

  • The problem is that on new posts there is no title so there would be nothing to set “default value” of the field to.

    Maybe I’m misunderstanding, can you provide more information on what you are attempting to do?

  • Hm, you are right. This could be a problem.
    So maybe i can change it to “if a field is empty, put the page-title into it“.

  • It really depends on what you are trying to do, what the ultimate goal is.

    Do you want this to be the default value and have it displayed on the site if nothing is entered into your field? or do you want to override whatever is in this field with the default value? Something else?

    You might want to take a look at the acf/load_value filter https://www.advancedcustomfields.com/resources/acf-load_value/

  • Hi John,

    sorry for this long interruption. The ultimate goal is:
    If i save the post/page/customposttype:
    – If nothing is entered in that field, a function insert the name of the post/page/customposttype in to the acf-field.

    I’m sure, it’s easy, but didn’t found the right solution.

    Thank you
    Stephan.

  • 
    add_action('acf/save_post', 'maybe_copy_title_to_field', 20);
    function maybe_copy_title_to_field($post_id) {
      $value = get_field('custom_field_name', $post_id);
      if (!$value) {
        $value = get_the_title($post_id);
        update_field('custom_field_name', $value, $post_id);
      }
    }
    
  • Cool, this works perfect!!!

    Thank you!

    Stephan.

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

The topic ‘Use post title as default value’ is closed to new replies.