Skip Navigation

[Resolved] Select Author changes on update although no Author selected.

This thread is resolved. Here is a description of the problem and solution.

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 support ticket is created 5 years, 10 months 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 4 replies, has 2 voices.

Last updated by Nashaat 5 years, 10 months ago.

Assisted by: Christian Cox.

Author
Posts
#1178595

Hi,
i am using followiing code to change author with cred form.

// Save Selected Author
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) ){
  
  
$my_post = array(
      'ID'           => $post_id,
      'post_author' => $_POST['my_autor_select']
  );
   
// Update the post into the database
  wp_update_post( $my_post );
 
}
}



// Clean View Output
  
add_filter( 'wpv_filter_wpv_view_shortcode_output', 'prefix_clean_view_output', 5, 2 );
      
function prefix_clean_view_output( $out, $id ) {
    if ( $id == '8650' ) { //Please adjust to your Views ID
        $start = strpos( $out, '<!-- wpv-loop-start -->' );
        if ( 
            $start !== false
            && strrpos( $out, '<!-- wpv-loop-end -->', $start ) !== false
        ) {
            $start = $start + strlen( '<!-- wpv-loop-start -->' );
            $out = substr( $out , $start );
            $end = strrpos( $out, '<!-- wpv-loop-end -->' );
            $out = substr( $out, 0, $end );
        }
    }
    return $out;
}

the following field is added to the post form to change the author.

      [cred_show_group if="($(car-status) eq  'sold )"  mode='fade-slide']

        <div class='col-12 text-white bg-success p-5 mb-4 rounded-10'>

            [cred_generic_field field="my_autor_select" type="select" class="form-control" urlparam=""]
            {
            "required":1,
            "validate_format":0,
            "persist":1,
            "default":[],
            "options":[ [wpv-view name="authors-select"] ]
            }
            [/cred_generic_field]

          </div>

      [/cred_show_group]

the problem is, even if this generic field is deleted from the form. the Author changes to current user who is editing the form each time.

The generic field as you can see its only an option depending on another option. this means only if one option is true then the user who is editing can chose one of the user as author.

#1178623

The code here modifies the post author regardless of whether or not the User has selected a value in the author field, or even if the field value doesn't exist in the $_POST superglobal:

$my_post = array(
      'ID'           => $post_id,
      'post_author' => $_POST['my_autor_select']
  );

If the my_autor_select value does not exist, this means the post author is undefined, which is probably the main problem here. Instead, you could pass in the current post author's ID, or you could remove the post_author key from the my_post array.

$post_author_id = get_post_field( 'post_author', $post_id );
#1178649

Thanks for your quick reply Christian.

If i remove the post_author key, then the my_autor_select wont change the author of the post anymore.

I am trying to change the Author only if an option is selected in my_autor_select. If not then it should keep the same current author.

If you have an idea how to make this work it will be very helpful.

#1178651

If i remove the post_author key, then the my_autor_select wont change the author of the post anymore.
What if you try something like this:

$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
  );
#1179361

Thank you Christian this solved the issue.