Support

Account

Home Forums General Issues Save total CPT count to ACF field

Solving

Save total CPT count to ACF field

  • Hey guys,
    posted this over on the WordPress.org forums (https://wordpress.org/support/topic/sequential-post-id-per-cpt/#post-9706469), 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.

    Thanks

  • 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
        if(empty($_POST['acf'])){
            return;
        }
    
    	/***************************
    	Structure of settings array.
    
    	array(
    		[post_type]=>[array(
    			[id_field_name]=>[value]
    			[starting_id]=>[value]
    		)
    	)
    
    	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.
    
    	*************************/
    
    	$settings=array(
    		'exporting'=>array(
    			'id_field_name'=>'import_id',
    			'starting_id'=>1
    		),
    		'acquisition'=>array(
    			'id_field_name'=>'export_id',
    			'starting_id'=>1
    		)
    	);
    
    	$id_field_form=null;
    	$id_field_name=null;
    	$post_type=null;
    
    	//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){
    		$field_data=get_field_object($form_name);
    
    		foreach($settings as $post_type_setting => $setting){
    			if($field_data['name']===$setting['id_field_name']){
    				$id_field_form=$form_name;
    				$id_field_name=$setting['id_field_name'];
    				$post_type=$post_type_setting;
    				break 2;
    			}
    		}
    	}
    
    	if($id_field_form===null||$id_field_name===null||$post_type===null||is_numeric($_POST['acf'][$id_field_form])){
    		//not the right post type or this is an update
    		return;
    	}
    
    	//new post save - get last id and increment;
    	$last_post=get_posts(array(
    		'posts_per_page'   => 1,
    		'offset'           => 1,
    		'orderby'          => 'date',
    		'order'            => 'DESC',
    		'post_type'        => $post_type
    	));
    
    	$generated_id=null;
    
    	if(count($last_post)>0){
    		//Posts have already been created of this type. Retrieve last post id and increment
    		$last_post=get_fields(array_pop($last_post));
    		$last_id=$last_post[$id_field_name];
    		$generated_id=$last_id+1;
    	}
    	else{
    		//This is the first post of the this type. ID will be set to the starting id.
    		$generated_id=$settings[$post_type]['starting_id'];
    	}
    
    	//set the new id to the form data and exit
    	$_POST['acf'][$id_field_form]=$generated_id;
    	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.