Skip Navigation

[Resolved] Update parent post author when creating or editing child post

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

Problem: I would like to update the post author in a related post when a Form is submitted to edit a child post.

Solution: Use wp_update_post in the cred_save_data callback to update a post author. Use the toolset_get_related_post API to get the parent post ID.

$relationship_slug = 'your-post-relationship-slug';
$parent_id = toolset_get_related_post( $post_id, $relationship_slug );
$my_post = array(
  'ID'           => 123,
  'post_author' => 789
);
wp_update_post( $my_post );

Relevant Documentation:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_post
https://developer.wordpress.org/reference/functions/wp_update_post/

This support ticket is created 4 years, 1 month 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 14 replies, has 2 voices.

Last updated by jamesR-13 4 years ago.

Assisted by: Christian Cox.

Author
Posts
#1899729

Trying to update parent post author when creating or editing child post

Here is my php code:

		if ( $form_data['id'] == 363 ) {
			$user_id = get_current_user_id();
        	        $parent_id = get_post_meta($post_id, '_wpcf_belongs_unit_id', true); // unit = your parent post type name
                     	update_post_meta($parent_id, 'post_author', $user_id); // wpcf-status = your field name
        	// user_id = your field new value  
        }
#1900367
relationships.png

Hello, I'll be glad to help. I see two main issues based on your comments and code sample so far:
1. Since Types 3, a new post relationships system is available. If you migrate to the new post relationships system, post relationships will no longer be stored using the _wpcf_belongs_{slug}_id meta key. After migration, you can no longer rely on this meta key/value pair to get the parent post, given the child post ID. I would need to know whether or not your site has migrated to the new post relationships system to determine whether or not this code will get the parent ID in a reliable way:

$parent_id = get_post_meta($post_id, '_wpcf_belongs_unit_id', true); // unit = your parent post type name

If you are unsure about whether or not your site has migrated to the new system, please go to Toolset > Relationships. If your site has already migrated to the new relationships system, you should see a list of all the post relationships you have currently set up on the site like the screenshot attached here relationships.png. If your site has not migrated yet, you should see a screen explaining the migration process and the steps you would need to take to migrate to the new system. Please take a screenshot of your screen at Toolset > Relationships and include it in your next reply, then I can give you some additional feedback about the best method for determining the parent post ID using PHP.

2. The standard WordPress post author information is not a custom field value, rather it is part of the post object itself. The function update_post_meta will not help you modify the standard post author, as it is only intended for modifying custom field values. I assume you want to update the standard WordPress post author, not a separate custom field like a User field created in Toolset Types. If this assumption is correct, you must change this line:

update_post_meta($parent_id, 'post_author', $user_id); // wpcf-status = your field name

Instead you must use the function wp_update_post to set the post author programmatically, since the post author is not stored in a custom field value. An example showing how to update the standard post author information:

$my_post = array(
  'ID'           => 123,
  'post_author' => 789
);
wp_update_post( $my_post );

You would replace 123 with the ID of the post (or more likely a variable representing that ID) whose author you would like to change. You would replace 789 with the ID of the User (again, more likely a variable representing that ID) you would like to set as the post author.

I'll stand by for your update and give you some additional feedback after reviewing your comments.

#1900595

It's a brand new site, so it would be built with the new relationships.

#1901265

To get the parent post ID, you can use the Post Relationships API function toolset_get_related_post:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_post
Example:

$relationship_slug = 'your-post-relationship-slug';
$parent_id = toolset_get_related_post( $post_id, $relationship_slug );

Replace your-post-relationship-slug with the slug of this post relationship. You can find the slug in Toolset > Relationships when you edit the post relationship.

See #2 in my previous comment for an example showing how to update the post author.

#1901867

OK, based on your help so far, here's what I have:


		if ( $form_data['id'] == 350 ) {
			$user_id = get_current_user_id();
        	$relationship_slug = 'unit-to-document';
			$parent_id = toolset_get_related_post( $post_id, $relationship_slug );
        	wp_update_post($parent_id, 'post_author', $user_id); 
        }

Unfortunately, the parent post author is still not being updated when adding a child post. I'm sure i'm missing something simple or doing something wrong. Please advise. Thank you.

#1902627

Looks like you've changed the way wp_update_post is called, passing the post ID and author information into the function as different parameters:

wp_update_post($parent_id, 'post_author', $user_id); 

It looks like it would work, similar to how wp_update_postmeta works, but it's not formatted correctly so it won't work like this. The example I shared shows the proper format and syntax for wp_update_post. You must put the post ID and post_author ID information in an array, then pass that array into the wp_upate_post function as the first parameter:

$my_post = array(
  'ID'           => 123,
  'post_author' => 789
);
wp_update_post( $my_post );

You can review the documentation for the wp_update_post function here:
https://developer.wordpress.org/reference/functions/wp_update_post/
The first parameter should be an array of post information, the second and third arguments are optional, but if used, should be booleans ( true or false ).

#1904627

I changed my parameters per your feedback, and still no joy. What am I missing?

	
		//update parent post author when adding or editing children
		if ( $form_data['id'] == 1531 ) {
			$user_id = get_current_user_id();
        	$relationship_slug = 'unit-to-document';
			$parent_id = toolset_get_related_post( $post_id, $relationship_slug );
        	wp_update_post($parent_id, 'post_author', $user_id); 
			$my_post = array(
				  'ID'           => $parent_id,
				  'post_author' => $user_id
				);
			wp_update_post( $my_post );
        }
#1905175

May I log in and see how this is set up? I think I must be missing something.

#1906657

I get an error when I try to log in with these credentials: "Unknown username. Check again or try your email address."

Can you double check?

#1906735

Oops! Try it again now. Same credentials. Thank you!

#1906785

Is it okay for me to create a new Unit Document to test this out? Or would you prefer to set up a staging site where we can run tests without disturbing the live site?

#1906787

You can create one. Just use unit 0000 please.

#1906825

Okay it looks like there was an extra closing bracket that was preventing this code from firing. I've fixed that and changed the code a bit:

//update parent post author when adding or editing children
		if ( $form_data['id'] == 1531 ) {
			$user_id = get_current_user_id();
        	$parent_id = $_POST['@unit-to-document_parent'];
        	$my_post = array(
				  'ID'           => $parent_id,
				  'post_author' => $user_id
				);
			wp_update_post( $my_post );
        }

Now I can see the parent Unit author being updated when I add a new Unit Document post. Can you confirm?

#1906827
Screen Shot 2021-01-15 at 11.54.50 AM.png

Screenshot showing extra closing bracket is attached here.

#1906893

See... I knew it was something stupid I was doing.

:facepalm:

My issue is resolved now. Thank you!