autoincrement a number field
-
I want to use ACF on our products (as product id ) – which would be something different from the id created in woocommerce. How can we use ACF with type number to create a field that would be unique and autoincrement whenever a new product is added.
-
I did something similar a while ago for an invoice system, which had to count up to the next unique invoice id. I haven’t found a build in solution so I made my own. I tried my best to comment on whats happening – you obviously need to update the code with your field-, function and id-names:
<?php function wfp_documents_setup_id_incr() { // Check if user has rights to set it up and ACF is enabled. if (current_user_can('administrator') && function_exists('get_field')): // Initial value // === YOU NEED TO UPDATE HERE === // Replace <code>custom_invoice_id</code> with your desired id name. add_option('custom_invoice_id', '0001', '', 'yes'); /** * Wrapper to get the id (if i would need to add something to it) * @return mixed|void – stored next available id */ function wfp_get_current_invoice_id() { return get_option('custom_invoice_id'); } /** * Count up the id by one and update the globally stored id */ function wfp_increase_invoice_id() { $curr_invoice_id = get_option('custom_invoice_id'); $next_invoice_id = intval($curr_invoice_id) + 1; update_option('custom_invoice_id', $next_invoice_id); } /** * Populate the acf field when loading it. */ function wfp_auto_get_current_document_id($value, $post_id, $field) { // If the custom field already has a value in it, just return this one (we don't want to overwrite it every single time) if ($value !== null && $value !== "") { return $value; } // If the field is empty, get the currently stored next available id and fill it in the field. $value = wfp_get_current_invoice_id(); return $value; } // Use ACF hooks to populate the field on load // ==== YOU NEED TO UPDATE HERE ==== // Replace <code>invoice_id</code> with the name of your field. add_filter('acf/load_value/name=invoice_id', 'wfp_auto_get_current_document_id', 10, 3); /** * Registers function to check if the globally stored id needs to be updated after a post is saved. */ function wfp_acf_save_post($post_id) { // Check if the post had any ACF to begin with. if (!isset($_POST['acf'])) { return; } $fields = $_POST['acf']; // Only move to the next id when any ACF fields were added to that post, otherwise this might be an accident and would skip an id. if ($_POST['_acf_changed'] == 0) { return; } // Next we need to find the field out id is stored in foreach ($fields as $field_key => $value) { $field_object = get_field_object($field_key); /** * If we found our field and the value of that field is the same as our currently "next available id" – * we need to increase this id, so the next post doesn't use the same id. */ if ($field_object['name'] == "invoice_id" && wfp_get_current_invoice_id() == $value) { wfp_increase_invoice_id(); return; } } } // Use a hook to run this function every time a post is saved. add_action('acf/save_post', 'wfp_acf_save_post', 20); /** * The code below just displays the currently stored next id on an acf-options-page, * so it's easy to see which id you're on. The field is disabled to prevent easy tinkering with the id. */ function wfp_load_current_document_ids_settingspage($value, $postid, $field) { if ($field['name'] == "document_ids-group_current_invoice_id") { return wfp_get_current_invoice_id(); } return $value; } function wfp_disable_acf_field($field) { $field['disabled'] = 1; return $field; } add_filter('acf/load_field/name=current_invoice_id', 'wfp_disable_acf_field', 10, 3); add_filter('acf/load_value/name=current_invoice_id', 'wfp_load_current_document_ids_settingspage', 10, 3); endif; } add_action('init', 'wfp_documents_setup_id_incr');
It worked perfectly for me, I hope it helps you too.
Hello!!
Very interesting your answer @workforpizza , but I would like to know what file you modified to include the functions, I would like to do some tests.
Thank you
Just put that code into the functions.php of your theme, or include it in a plugin.
@workforpizza – I will be telling you.
Thanks ??
@workforpizza
Thank you for this..I’d like this to increment a reference number on a specific custom post type, it seems this will work for every post? how do I restrict it to just post-type-x?
ignore that question, I have a different one – how can I reset the current id once it’s started?
I’ve been testing, but need to change the initial id and changing it at the initial value doesn’t seem to work…
@boldfish the initial value doesn’t work because it uses the
add_option
function, which only works if the option does not exist yet.So you just create a function and use the
update_option
function to reset it to your starting value, for example:function wfp_reset_id() { update_option('custom_invoice_id', '0001'); }
And then call this function whenever you want to reset the ID.
-
This reply was modified 5 years, 4 months ago by
workforpizza.
@workforpizza aha, my ignorance is on full display! thanks for that, all makes sense now.
:o)
One more question if I may…
The options page – bit lost with that – do I need to create the page first? what’s it called? can’t break down the syntax.
Thanks
@boldfish the code in my example has nothing to do with specifically an options page, it just prefills a custom field that has the key “current_invoice_id” with the current ID when its loaded.
Where you add this custom field is up to you. You could add it to a regular set of custom fields on a page or create an options page (see on: https://www.advancedcustomfields.com/resources/acf_add_options_page/) and then display your set of custom fields on that options page.
I hope this makes sense for you.
it was the bit after this that was puzzling me:
/**
* The code below just displays the currently stored next id on an acf-options-page,
* so it’s easy to see which id you’re on. The field is disabled to prevent easy tinkering with the id.
*/
function wfp_load_current_document_ids_settingspage($value, $postid, $field)
{I couldn’t work out how to tell it which page to display the count on, but if I read you right then adding the field name to an existing options page will pick up the current value?
@boldfish correct, nothing in the code tells wordpress to display the field anywhere. You have to do the displaying through regular custom fields groups.
The code only looks for any field anywhere with the given name and prefills it.
What would happen if multiple requests were to be processed at the exact same time?
-
This reply was modified 5 years, 4 months ago by
- The topic ‘autoincrement a number field’ is closed to new replies.