Home › Forums › Backend Issues (wp-admin) › Set Post Author from Custom Field Value
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?
Figured it out, adding the code that worked for me in case this can help someone else.
/**
* Use ACF custom field to set post author for Project CPT
*/
add_action( 'acf/save_post', 'project_author_save_post', 20 );
function project_author_save_post( $post_id ) {
$project_owner = get_field( 'project_owner', $post_id );
if($project_owner) {
$link = get_post_meta( $project_owner->ID, 'user_link', true );
}
if($link) {
wp_update_post( array( 'ID'=>$post_id, 'post_author'=>$link['ID']));
}
}
You must be logged in to reply to this topic.
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!
🚨 The 2023 ACF Annual Survey closes tomorrow! This is your last chance to complete the survey and help guide the evolution of ACF. https://t.co/0cgr9ZFOJ5
— Advanced Custom Fields (@wp_acf) May 18, 2023
© 2023 Advanced Custom Fields.
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.