Home Forums General Issues Save total CPT count to ACF field


Save total CPT count to ACF field

  • Hey guys,
    posted this over on the forums (, but getting nowhere. Im looking for some help on a custom function, or failing that, to hire somebody to help implement it if need be.

    Also, i hope this is the correct place for help/requests. (apologies if its not)

    I have 2 CPT’s in WordPress, id like them both to have an independent total count number, that simply adds +1 as each knew post is created.

    I want this number to be saved in to an ACF field, and never be altered.

    All the numbers need to be sequential for each CPT (no skipping numbers).

    If anybody has any suggestions, idea’s or able to lend a hand, it would be massively appreciated.


  • In order to save the total number of posts each time one is created, I think you might need to hook into either acf/save_post or WordPress save_post, where you would update the total count.

    Depending on your requirements, it may be easier to just use wp_count_posts when you need to display that number.

  • Thought id update this thread as I ended up working direct with another developer on this.

    He wrote a custom function, which relies on a new ACF field that stores the count data.

    I hope it helps somebody else!

    function generateInvoiceId($post_id) {
    	// bail early if no ACF data
    	Structure of settings array.
    	The 'id_field_name' is the name of the ID field that you created in admin.
    	They MUST be unique so the post type can be determined correctly.
    	//find which form field is the relavant id field and use it to determine which settings to use
    	foreach($_POST['acf'] as $form_name => $field){
    		foreach($settings as $post_type_setting => $setting){
    				break 2;
    		//not the right post type or this is an update
    	//new post save - get last id and increment;
    		'posts_per_page'   => 1,
    		'offset'           => 1,
    		'orderby'          => 'date',
    		'order'            => 'DESC',
    		'post_type'        => $post_type
    		//Posts have already been created of this type. Retrieve last post id and increment
    		//This is the first post of the this type. ID will be set to the starting id.
    	//set the new id to the form data and exit
    	return true;
    add_action('acf/save_post', 'generateInvoiceId', 1);
Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Save total CPT count to ACF field’ is closed to new replies.