I would like to set/ save Post Author using custom field/ meta value related to another custom post type.
Custom Post Type #1 = “Projects” with various custom fields: including “Project Owner” which is an ACF Post Object (single value) field for “Members” CPT.
Custom Post Type #2 = “Members” with various custom fields: including “User Link” using ACF User field type (set to User ID return value). Post Author of each “Member” is also set to the corresponding member/ user.
So I see 2 potential options here:
1. Maybe I can use “Project Owner” CF to pull Post Author ID from related “Member” post – and use that data for Post Author of “Project” CPT.
2. Alternatively, maybe I can use “Project Owner” CF to pull meta values from “User Link” and related “Member” post – including “Member Profile Name” CF to display member’s name.
It would seem that I need to use ACF Action acf/save_post in order to set/ save Post Author, so I tried modifying some code I found below:
function my_acf_save_post( $post_id ) {
// get new value
$user = get_field( 'project_owner', $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);
But this did not work – likely because not showing relational post object among other issues.
I also tried using this code:
function my_acf_save_post( $post_id ) {
$related_author_id = get_field('project_owner', $post_object->ID);
$value = array(
'ID' => $post_id,
'post_author' => $related_author_id
);
wp_update_post( $value );
}
add_action('acf/save_post', 'my_acf_save_post', 20);
But that was no good either.
Is it even possible to set/ save Post Author from custom field of a related post and/ or pull meta values from this data to populate Post Author values?