Skip Navigation

[Resolved] How to change Post Author of CPT (via Post Form) to a custom field value on save

This support ticket is created 3 years ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 7 replies, has 3 voices.

Last updated by Christian Cox 2 years, 12 months ago.

Assisted by: Christian Cox.

Author
Posts
#2026327

Tell us what you are trying to do?
I have a custom post type (called 'Reference') which I am entering using a Toolset 'Post Form' on the front end. Naturally when I enter this post, the post author goes in as my logged in profile. However, what I want in this case, is for the post author to change to the value of a custom field (which is in fact a 'relationship' field - the slug of which is 'client-reference'), when the post gets saved.

So, just to explain further:

- on my website, I have User Accounts for Staff (with a User Role of 'Staff'). Staff need to be able to input 'References' (a custom post type).
- on my website, I also have another type of user, a 'client' (these have their own User Role of 'Client'). Due to my need to have relationships between 'Clients' and 'References', I also have a Custom Post Type of 'Client', which mirrors the User if you like.
- I have a 'Relationship ' setup between my 'Client' Custom Post Type and my 'Reference' Custom Post Type (this is a one to many relationship, ie 1 client can have multiple References).
- 'References' are input by 'Staff' users, using a 'Post Form' on the front end of my website. As part of the Post Form, the 'Client Reference' relationship field displays, and the Staff user can select a 'Client' (from my Client Custom Post Type) to assign this Reference to. The 'Client' is always an email address. So basically, the Staff inputting the Reference can assign the Client to the Reference by selecting the clients email address from the 'Client Reference' field. This is all working perfectly so far as I want.

- However, when saving the Reference Post, the 'Post Author' of the 'Reference' shows as the logged in user (the Staff). What I want, is for the 'Client Reference' relationship field (ie the Clients email address) to populate the Post Author field (because I am using the Post Author to display posts on different parts of the website). I have tried entering the following code into my functions.php file:

add_action('cred_save_data', 'my_update_data_activity',10,2);
function my_update_data_activity($post_id, $form_data)
{

if ($form_data['id']==57) {

// Create post object
$my_post = array(
'ID' => $post_id,
'post_author' => 'client-reference'
);

// Update the post into the database and save new Activity post ID
wp_update_post( $my_post );

}
}

However this does not work, and the post author field is just blank after I submit the form. I made this code by taking code provided from another support thread of mine (https://toolset.com/forums/topic/how-to-update-a-custom-field-in-a-cpt-post-with-data-from-a-user-form/#post-2026209) and then changing it to use the 'wp_update_post' (instead of 'wp_insert_post) as described here = https://developer.wordpress.org/reference/functions/wp_update_post/ however clearly what I have coded is incorrect and does not work.

Please can you help provide me with a solution to achieve what I need?

Thanks very much for your help,

Keith

Is there any documentation that you are following? Mentioned above

Is there a similar example that we can see? No

What is the link to your site? Under development, however I can provide a link and Admin access if you give me a private box to supply the details in 🙂

#2026519

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+00:00)

Hello Keith and thank you for contacting the Toolset support.

Well, the code that you are using is not correct. You need to pass the user ID to "post_author" instead of "client-reference". You need to update the code to introduce two steps. One where you will get the client post, then a step to get the user ID from the client post.

You'll use the toolset_get_related_post function to get the client post. Assuming that the relationship slug is 'client-reference', the code will be:

$client_post = toolset_get_related_post($post_id, 'client-reference', 'parent');

https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_post
Assuming that the user is the author of the client post, you can get it using:

$user_id = $client_post->post_author;

Then you can save it:

$my_post = array(
    'ID' => $post_id,
    'post_author' => $user_id,
);

// Update the post into the database and save new Activity post ID
wp_update_post( $my_post );

If you are unsure about something, allow me temporary access and I'll check it and get back to you. Your next reply will be private to let you share credentials safely. ** Make a database backup before sharing credentials. **

#2027485

Just to update the thread (as well as the info provided in the private box which isn't visible), I think this is a problem with the code:

toolset_get_related_post($post_id, 'client-reference', 'parent');

That just seems to not return anything. I am having the same issue with that toolset_get_related_post function not working with something else (I will make a separate support question about that too) - it's very frustrating, I'm not sure why that function is not working?

Anyway, I thought I would add that information on - it may turn out to be something different, but the fact that is not working in another code snippet I am trying is a large coincidance if not.

I look forward to your update.

Many thanks,

Keith

#2028335

Hi Jamal, is there any update on this please?

Many thanks,
Keith

#2028929

Hi there, I just thought I would add a further update. I had a similar problem in a separate ticket (https://toolset.com/forums/topic/how-to-retrieve-cpt-post-title-of-related-post-to-use-in-another-post/) where Christian suggested I try the 'cred_submit_complete' hook instead, and that fixed the problem.

With that in mind, I tried to modify my code for this thread to use the same thinking (I changed the hook and then tried to use 'get_the_author' to get the author from the related parent post id) - this is the new code I have tried:

add_action('cred_submit_complete', 'my_update_data_activity',10,2);
function my_update_data_activity($post_id, $form_data)
{

if ($form_data['id']==57) {

$km_post_id = toolset_get_related_post($post_id, 'client-reference', 'parent');
$km_post_author = get_the_author($km_post_id);

// Create post object
$my_post = array(
'ID' => $post_id,
'post_author' => $km_post_author
);

// Update the post into the database and save new Activity post ID
wp_update_post( $my_post );

}
}

But unfortunately, this code did not work either 🙁 The author is still incorrect after inputting a Reference (the author is now being input as my administrator user role account instead of the staff user account it was before, and still not the 'client' account test1@akcreation.co.uk which I am expecting). I would really appreciate an update on this if possible please.

Thanks very much,

Keith

#2028943

The function get_the_author returns the author's display name:

$km_post_id = toolset_get_related_post($post_id, 'client-reference', 'parent');
$km_post_author = get_the_author($km_post_id);

https://developer.wordpress.org/reference/functions/get_the_author/

But you need the author ID here:

// Create post object
$my_post = array(
'ID' => $post_id,
'post_author' => $km_post_author
);
// Update the post into the database and save new Activity post ID
wp_update_post( $my_post );

So I suggest you try the following adjustment:

$km_post_id = toolset_get_related_post($post_id, 'client-reference', 'parent');
$km_post_author_id = get_post_field('post_author', $km_post_id);
// Create post object
$my_post = array(
'ID' => $post_id,
'post_author' => $km_post_author_id
);
// Update the post into the database and save new Activity post ID
wp_update_post( $my_post );

If that doesn't work, we'll need to turn on error logging to get more information.

#2028953

Christian - you are brilliant! I cannot thank you enough 🙂 I've been tearing my hair out over this for days now.
I can confirm that it now works, the Reference post author is going in as the related client user.

I just wanted to say thank you also for explaining why the other code did not work, it really helps me to try and understand how to figure things out better myself for next time. And finally, thank you also for jumping over from the other ticket (I know you didn't have to do that and I really appreciate it). I can sleep well tonight now haha.

Have a great day!

#2028973

No problem, glad to get that resolved for you. Have a good night!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.