Support

Account

Home Forums General Issues Pre populate user field with current user Reply To: Pre populate user field with current user

  • Hi Phil,

    Thank’s for the push in the right direction but the acf-load, did not work as I did it on a new post, it gave me the field before it was loaded (as the name suggest I suppose :-)) and messed up the load of all users.

    Maybe it has a solution but I achieved what I needed with the below one (don’t know if it is “programatically correct” but it works for me) if anyone else have the same problem:

    function my_prepare_field( $field ) {
    $screen = get_current_screen();
    $user = wp_get_current_user();
    $user = $user -> ID;

    //check if post type is new and of a specific post type
    if ( ‘add’ === $screen->action && “my_post_type” === $screen->post_type ){
    $field [‘value’]= $user; //set user to current user
    return $field;
    }
    else {
    return $field;
    }

    }

    add_filter(‘acf/prepare_field/name=my_post_type’, ‘my_prepare_field’);