Problem: I am using Forms to allow Users to edit a post. I would like to give them the opportunity to modify the post author, so I created a generic field that contains the author options. If the default option is selected, I would like to keep the existing post author. If another option is selected, I would like to update the author to be the selected User. My cred_save_data hook doesn't seem to work correctly because sometimes the current User becomes the post author even if nothing is selected.
Solution:
You should modify the code so that the original author is included in the wp_update_post arguments if nothing is selected in the generic field. Here's an example:
add_action('cred_save_data', 'my_save_data_action_select_author',10,2); function my_save_data_action_select_author($post_id, $form_data) { //create an array of values with all ID's of the Forms you want to check: $ids = array("8604","8598","8603","8599"); //Check if the current form ID is one of in the above array: if (in_array($form_data['id'], $ids) ){ $author_id = isset( $_POST['my_autor_select'] ) ? $_POST['my_autor_select'] : get_post_field( 'post_author', $post_id ); $my_post = array( 'ID' => $post_id, 'post_author' => $author_id ); // Update the post into the database wp_update_post( $my_post ); } }
Relevant Documentation:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
https://codex.wordpress.org/Function_Reference/wp_update_post
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 4 replies, has 2 voices.
Last updated by 5 years, 10 months ago.
Assisted by: Christian Cox.