Skip Navigation

[Resolved] Cred form add post – insert author from populated relationship field

This support ticket is created 2 years, 4 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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 5 replies, has 2 voices.

Last updated by TomW5440 2 years, 4 months ago.

Assisted by: Minesh.

Author
Posts
#2426105
Untitled.png

Tell us what you are trying to do?
When adding new post form I'm looking to insert the author from a populated relationship field in the form which contains the name of the author.

I'm following the guide below which is similar to what I'm after but I don't think I need to create a view as the data that I need is already populated in the relationship field in the same form.

Is there any documentation that you are following?
https://toolset.com/forums/topic/cred-form-to-select-author/

Here's the field:

[cred_field field='@agent-entry.parent' value='[wpv-post-id item="@agent-property.parent"]' class='form-control' output='bootstrap' select_text='--- not set ---']

Here's the function:
add_action( 'cred_save_data', 'my_save_data_action', 10, 2 );
function my_save_data_action($post_id, $form_data) {
// if a specific form
if ( $form_data[ 'id' ] == 401950 ) { //Make sure you set the correct ID of your form
$my_post = array(
'ID' => $post_id,
'post_author' => $_POST['@property-entry.parent']
);

// Update the post author ID into the database
wp_update_post( $my_post );

}
}

This kind of works but only if a user is logged in. This form is for guests who do not log in and the author should be inserted from the data in the relationship field which is the agent.

Thank you!

#2426135

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Can you please share problem URL where you added the form as well as admin access details and let me see whats going wrong when you submit the post as guest user.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#2427173

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

The first issue was the parent field you need to add it before the submit button so I've moved it the Agent Entry parent field before the submit button as you can see now:
=> hidden link

Then, I've activated the snippet you added and changed the following line of code from:

  'post_author' => $_POST['@property-entry.parent']

To:

  'post_author' => $_POST['@property-entry_parent']

- The . (dot) is converted to _ (underscore)

Now, the actual code looks like this:

add_action( 'cred_save_data', 'my_save_data_action', 10, 2 );
function my_save_data_action($post_id, $form_data) {
    // if a specific form
    if ( $form_data[ 'id' ] == 401950 ) { //Make sure you set the correct ID of your for
      
        $my_post = array(
            'ID'           => $post_id,
            'post_author' => $_POST['@property-entry_parent']
        );
           
        // Update the post author ID into the database
        wp_update_post( $my_post );
        
    }
}

Could you please confirm now it works as expected. You can remove the other parent field from your form that is not required that is added after the submit button.

#2427691
minesh.png

Hi Minesh,
Thank you but it's still not adding the author to the entry post on form submission.
I need to get the entry agent name to be inserted as the author of the entry post on form submission.

#2427717

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Could you please check now.

I've adjusted the code as given under within the "Custom Code" section offered by Toolset:

add_action( 'cred_save_data', 'my_save_data_action', 10, 2 );
function my_save_data_action($post_id, $form_data) {
    // if a specific form
    if ( $form_data[ 'id' ] == 401950 ) { //Make sure you set the correct ID of your for
        
      $author_id = get_post_field ('post_author',$_POST['@agent-entry_parent']);

        $my_post = array(
            'ID'           => $post_id,
            'post_author' =>  $author_id
        );
           
        // Update the post author ID into the database
        wp_update_post( $my_post );
        
    }
}

I can see it sets the post author successfully now.

#2427719

My issue is resolved now. Thank you!