Support

Account

Home Forums General Issues CPT and adding a user using ACF Reply To: CPT and adding a user using ACF

  • After many days, I managed to solve this (although no thanks to this support forum which seems almost deprecated now)

    As I am using ACF, I should use the built-in acf/save_post to do what I want after the data is saved. As this action will be called every time a post is created/saved, etc, I also needed to add an if statement to check if it’s the post type I’m looking for.

    All of the fields should be available via $_POST['fields'] as well if get_post_meta does not return what I’m looking for.

    The modified code below uses the ACF action AFTER the post has been saved, so the meta should be available. I can do a var_dump on the $_POST['fields'] to see what POST data is available as well.

    http://www.advancedcustomfields.com/resources/actions/acfsave_post/

    add_action('acf/save_post', 'people_postdata', 20);
    function people_postdata($post_id) {
    	global $wpdb;
    	if ( $post_id && (get_post_type($post_id) == 'People') ) {
    		$firstname = get_post_meta($post_id, 'first_name', true);
    		$lastname = get_post_meta($post_id, 'last_name', true);
    		$email = get_post_meta($post_id, 'email_address', true);
    		$password = get_post_meta($post_id, 'password', true);
    		$username = preg_replace('/[^A-Za-z0-9]/', '', strtolower(get_the_title($post_id)));
    	    $userargs = array(
    	    	'first_name' => $firstname,
    	    	'last_name' => $lastname,
    	    	'user_login' => $username,
    	    	'user_email' => $email,
    	    	'user_pass' => $password,
    	    	'role' => 'basic'
    	    );
    	    //var_dump($userargs);
    		wp_insert_user($userargs);
    	}
    }