Skip Navigation

[Resolved] Programmatically set child post author to be the same as parent post author

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

Problem: I have a Form that creates child posts. It works, but I would like to set the post author to be the same as the parent post author.

Solution: In the CRED form for creating child post, there is a a parent post select field. You can access the selected option of the parent post field in the $_POST superglobal. The parameter name will be in the format "@relationship-slug_parent". Then in the cred_save_data hook, you can use the parent post ID to determine the correct post author ID. Try something like this:

$parent_post_id = $_POST['@relationship-slug_parent'];
$parent_author_id = get_post_field ('post_author', $parent_post_id);
$postarr = array(
  'ID'            => $post_id,
  'post_author'   => $parent_author_id
);
// MEDARBEJDER insert in database Insert 
wp_update_post( $postarr );

Replace relationship-slug with the actual slug of your post relationship. So if your post relationship is foo-bar, the parameter name will be @foo-bar_parent.

Relevant Documentation:
https://codex.wordpress.org/Function_Reference/get_post_field
https://codex.wordpress.org/Function_Reference/wp_update_post

This support ticket is created 5 years, 9 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 rexJ-2 5 years, 8 months ago.

Assisted by: Christian Cox.

Author
Posts
#1199241
wp_toolset_associations.jpg

Tell us what you are trying to do?
Trying to find out how to get data from wp_toolset_associations table, make a lookup in wp_posts and get post_author

I have an action that is working

 add_action('cred_save_data', 'after_save_data_for_form',30,2);
function after_save_data_for_form($post_id, $form_data)
{ //code here}
its also working fine with updating the form. Only i would like to get the field from   wp_toolset_associations table->id.parent_id and put that value
into  post_author
[php]
$postarr = array(
		'ID'			=> $post_id,
            // 'post_status'   => 'publish',
            'post_author'   => 'INSERT HERE  $child_id',
            //'post_parent'   => $customPostParent,
            //'comment_status' => 'closed',
            //'ping_status' => 'closed',
            /*'meta_input'   => array(
                '_wp_page_template' => 'default',
                '_wp_old_date' => '2018-12-04',
                'wpcf-tilknytning' => get_user_meta($post_id,'wpcf-tilknyttetafdeling',true),  
                'wpcf-medarbejderbillede' => get_user_meta($post_id,'wpcf-medarbejderbillede',true),   //wp_users field meta_key wpcf-medarbejderbillede
                ),*/
            //'post_category' => array( 3)
        );
        // MEDARBEJDER insert in database Insert 
         wp_update_post( $postarr );

Think i have to lookup in wp_toolset_associations table and then use it in wp_posts

//get the post whose child_id is =  $post_id.
		$parentPost = get_posts(array('post_author' => $child_id, 'post_type' => 'medarbejder'));
		foreach ($parentPost as $post) {
		    $title = get_the_title($post->ID);
 		    $child_id = $post->post_author;
		    $etid = $post->ID;
		    break; //use this to limit to a single result
		}

Wondering if is is something like that

 // Connect a custom Author post with the ID of "5" to a custom Book post with the ID of "7" in a post relationship between Books and Authors
toolset_connect_posts( 'book-author', 5, 7 );
function my_plugin_override() {
    toolset_connect_posts( $relationship, $parent, $child );
}

Or is there and easier way in Toolset Hooks. I’m a little stuck here!??
Many Regards
Is there any documentation that you are following?
https://toolset.com/forums/topic/creating-of-intermediary_id-in-wp_toolset_associations/
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_connect_posts

Is there a similar example that we can see?

What is the link to your site?
hidden link

#1199292

Hi, I read your code and comments but I don't quite understand what you want to accomplish. Please correct me if I'm wrong:
1. There is a Form that creates child posts in a one-to-many relationship.
2. The User can select a parent post in the Form.
3. When the Form is submitted, you would like to change the post_author of the child post.
4. The post_author of the child post should be the same as the post_author of the parent post.

Am I correct so far?

#1199337

Hi,
Yes correct with 1 , 2, 3. 4 🙂
If i understand toolset Types right it is not so easy to combine wp_users and a posttype in a relationship. So i have made a member post type where i copied all data from users table. then i have a time schedule type table and that is the relation. Then the Childtable (the time schedule) have a SELECT of member so that a member can have more appointments. The problem is that if i want to do a view with loop in appointments it uses the post_author to sort after and that works fine as long it is the member itself who is logged in and do the appointment, but if a staff do the booking the post_author get the value from the logged in staff and not from the SELECT field. Therefore i thought the solution is in the update statement to lookup the SELECT from the form get the id to wp_post and get the post_Author! Make sense or is there a easier way?

#1199374

In the CRED form for creating child post, there is a a parent post select field. You can access the selected option of the parent post field in the $_POST superglobal. The parameter name will be in the format "@relationship-slug_parent". Then in the cred_save_data hook, you can use the parent post ID to determine the correct post author ID. Try something like this:

$parent_post_id = $_POST['@relationship-slug_parent'];
$parent_author_id = get_post_field ('post_author', $parent_post_id);
$postarr = array(
  'ID'            => $post_id,
  'post_author'   => $parent_author_id
);
// MEDARBEJDER insert in database Insert 
wp_update_post( $postarr );

Replace relationship-slug with the actual slug of your post relationship. So if your post relationship is foo-bar, the parameter name will be @foo-bar_parent.

#1199770

Hi Christian, So many thanks for quick reply and precise notation. My issue is resolved now. Thank you!