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 🙂