Support

Account

Home Forums Backend Issues (wp-admin) Select Author with ACF Field?

Solved

Select Author with ACF Field?

  • Hey Guys,

    Could anyone tell me whether there is a way to replicate the post author select box with ACF? We have a very custom ACF options section on our post edit screen and I would like an author select box to appear with all of the other acf options on the page.

    Therefore I would like to add a select box which has all authors listed. And selecting an author sets them as the author for the current post. Currently i can include an author select box, but it does not set the actual post author when publishing.

    Many thanks
    Andrew

  • I have not tested this but it should be what you are looking for. You can simply use the user field type which returns the user id. This will override the default user id system with your custom one as it makes this change after the post was saved.

    function my_acf_save_post( $post_id ) {
        // get new value
        $user_id = get_field('CUSTOM_AUTHOR');
    	if($user_id){
    		wp_update_post( array( 'ID'=>$post_id, 'post_author'=>$user_id) ); 
    	}
    }
    add_action('acf/save_post', 'my_acf_save_post', 20);
  • Thank you very much sir, implemented it and it worked perfectly. Really appreciate your help!!

  • Hello,

    It appears that this isn’t working as i first thought. For some reason, when setting the author via ACF field, a custom author IS set but it sets the very first author in the system alphabetically, regardless of what i select in the dropdown.

    Much easier to explain this with a video, so I have uploaded a narrated video at this YouTube address: https://www.youtube.com/watch?v=WXck17Xc_Ng&feature=youtu.be

    Thank you very much in advance
    Andrew

  • function my_acf_save_post( $post_id ) {
        // get new value
        $user_id = get_field('author');
    	if($user_id){
    		wp_update_post( array( 'ID'=>$post_id, 'post_author'=>$user_id) ); 
    	}
    }
    add_action('acf/save_post', 'my_acf_save_post', 20);

    This is the code I am using

  • Hey,

    Just to follow up on this. A friend of mine was able to modify the code slightly, and this is the version which worked:

    function my_acf_save_post( $post_id ) {
        // get new value
        $user = get_field( 'author', $post_id );
    	if( $user ) {
    		wp_update_post( array( 'ID'=>$post_id, 'post_author'=>$user['ID']) ); 
    	}
    }
    add_action('acf/save_post', 'my_acf_save_post', 20);
  • Hi,
    I need this feature, but I don’t know where I should put this code.
    I tried on functions.php but it didn’t work.
    Thanks in advance for your help!

  • it needs to go in functions.php, make sure that the get_field ‘author’ matches up with the name of your ACF field, otherwise it won’t work.

  • You’re right, there was a typo.
    Thanks!

  • Hi again,
    Any idea about how to adapt this code to allow selecting multiple authors?
    I’ve tried to allow multiple values to that field, but only the first one is saved.

    Thanks again!

  • Thanks for this @metalpotatouk this is super helpful. This works almost perfectly but I’m still having a small issue. I’m using Buddyforms to create/edit front end posts with ACF managing the fields for a custom post type in the backend. I’ve added the code you shared to the functions.php file.

    When I try to assign to an author (other than the signed in user) and save the post the ACF field is updated correctly but this doesnt initially update the post_author field (the post is still assigned to the logged in user). If I go to the back end and open the post I can see the author field has been updated correctly and if I then press ‘update’ it will then update the post_author field correctly.

    Is there something that could trigger this update without me having to go into the backend and do it manually?

    Thanks

Viewing 11 posts - 1 through 11 (of 11 total)

You must be logged in to reply to this topic.

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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.