I’m building a quoting system and need to populate a custom text field (quote_number) with the post id when the new post is created. I can’t just use the post_id when displaying the post as I have a previous quoting website built with Toolset Types that have existing id’s that filled the quote number field.
I tried the code from this post: https://support.advancedcustomfields.com/forums/topic/automatically-populate-custom-field-with-post-id/
function acf_load_field_choices( $field ) {
// Create an empty array
$field[‘choices’] = array();
// Populate field with the current post id
$field[‘choices’][0] = get_the_ID();
// Return the result
return $field;
}
add_filter(‘acf/load_field/name=test’, ‘acf_load_field_choices’);
There was no explanation as to how they got it right.
The post type is ‘quote’ and the field to be populated is quote_number.
Can someone explain the code above and how to modify it for my site.
Thanks
The code you posted is for populating the choices of a select field and will not work for adding a value to a text field.
You cannot populate the post ID into a text field on a front end form, assuming you are using acf_form() before the new post is created. This could only be done after the post is created.
See acf/save_post
The post ID is passed to your action and then you can use update_field() to insert set the field.
Thanks, I realise the code is for select options. Can the code be modified to work with a text input. I am wanting to populate the quote_number field with the post_id after form submission. This will then be shown on the View Quote page after post creation as the unique quote number.