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’);
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.