Question

I am developing my site and require to change post author id on the fly while publishing.

All post will be write and publish by admin but as content provided by different author and need to change post author id from admin to other author ( author id will get from custom field )

So how to do this on the fly while publishing.

Does the author info have to come from the custom field? Because you could do this straight in the UI manually.

  • Enable the "author" block in the display options when creating a post

  • Change the author in the drop-down menu

Here is my theme metabox.php code I am using wpalchemy metabox script and below code is in my cpt loop php file

// getting custom field value from image_artist
// this is giving value like artist_login_name / id
$png_gallery_meta->the_field('image_artist');
$artist_info = $png_gallery_meta->get_the_value();

// to separate artist_login_name and id
$string = $artist_info;
$artist = substr($string, 0, stripos($string, "/") );

// getting first name and last name from user id
$author_first_name = get_userdata(basename($string))->first_name;

$author_last_name = get_userdata(basename($string))->last_name;

So I am using above code to get user info from custom field dropdown selection box. Now how can I use above value to change id on the fly while admin publish the post the post author id should change with above id value get from selection box. I hope now it is more clear.

Was it helpful?

Solution

$post_id = $post->ID; // change this to whathever
$user_id = '4'; // change this too

$the_post = array();
$the_post['ID'] = $post_id;
$the_post['post_author'] = $user_id;

wp_insert_post( $the_post );

The code above will update the currently looped through post (via $post->ID) with an author with ID of 4.

Read how wp_insert_post works here. Basically, if you pass it an ID of a post that already exists it will update that post with the new information you pass to it. (the author ID in this case)

If you want to grab things from the custom fields you can use:

$author = get_post_meta($this->ID, 'author_id', TRUE);

More info on custom fields.

OTHER TIPS

Does the author info have to come from the custom field? Because you could do this straight in the UI manually.

  • Enable the "author" block in the display options when creating a post

  • Change the author in the drop-down menu

Here is my theme metabox.php code I am using wpalchemy metabox script and below code is in my cpt loop php file

// getting custom field value from image_artist
// this is giving value like artist_login_name / id
$png_gallery_meta->the_field('image_artist');
$artist_info = $png_gallery_meta->get_the_value();

// to separate artist_login_name and id
$string = $artist_info;
$artist = substr($string, 0, stripos($string, "/") );

// getting first name and last name from user id
$author_first_name = get_userdata(basename($string))->first_name;

$author_last_name = get_userdata(basename($string))->last_name;

So I am using above code to get user info from custom field dropdown selection box. Now how can I use above value to change id on the fly while admin publish the post the post author id should change with above id value get from selection box. I hope now it is more clear.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top