Skip Navigation

[Resolved] Translate parent child posts

This support ticket is created 7 years, 6 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+01:00)

This topic contains 7 replies, has 2 voices.

Last updated by Nigel 7 years, 5 months ago.

Assisted by: Nigel.

Author
Posts
#448455

Hi,
I'm translating a site with WPML. My parent posts need to be translated, but my child posts do not.
I'd hoped that when I create the translation of the parent and 'Copy content' to the new language version, my children would also be copied. This is not happening.

There are 200+ parents on the site and 1100 children. Do I really need to translate the children and then manually assign them to the new parent? Any other solution?

Thanks,

#448831

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Hi Simon

When you copy content or duplicate a post in WPML when creating a translation it does not copy across the post meta, which is where the parent / child relationship is stored.

So, the short answer is that, yes, you have to manually re-set the parents of the child posts.

But the prospect of having to do that on your site is a bit grim, and computers are supposed to be quite good at automating things, so let's see if we can find a better solution.

I need to do some digging, and I will get back to you when I have something to suggest.

#448863

Thanks Nigel.
That's not great news - I'd hoped Toolset and WPML would work a little closer on this one.

I'm guessing that I will have to translate the child posts (i.e. create an individual translation for each) so that I can create the parent child relationship. Otherwise I'm not going to be able to associate my single child with both language parents.

So I have two large manual tasks ahead.

1) Create 1100 unnecessary child post translations
2) Associate 1100 child posts with their parents.

#448899

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Hi Simon

I did some digging—I know Toolset much better than I know WPML these days—and recall that you can set WPML to copy across custom fields when it creates a translation. For hidden fields, such as _wpcf_belongs-parent-slug_id, you need to go to WPML > Translation Management > Multilingual Content Setup and under Custom Field Translation click Show System Fields where you should then see the relevant field to copy across to the translation.

Which would be great, except in your particular situation it won't help. If you create a translation of the child post it will automatically copy across the post meta that specifies the parent post, keeping the relationship intact.

But you only want to translate the parent posts, and the parent post doesn't record the relationship with the child, and so there is nothing to copy.

The solution I envisage would involve hooking into the copy content step on the parent and adding some custom code to set the required meta on the child posts.

I don't see a documented hook for that, I will look and see if I can find something undocumented. Again, I will update you with my findings.

#449783

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Hi Simon

I spent some time getting a basic version of this to work.

There are perhaps a number of ways you might go about it.

I have come up with some code that works with a particular workflow, if your requirements are different then you may want to modify it.

Firstly, the code, which should be added to your theme's functions.php file:

add_action( 'icl_copy_from_original', 'duplicate_child_posts' );
function duplicate_child_posts( $parent_id ) {

	// All we have to go on is the original language parent post id
	$parent_post_type = get_post_type( $parent_id );
	$target_lang = apply_filters( 'wpml_current_language', NULL );

	// get the translated parent post id
	$translated_parent_id = apply_filters( 'wpml_object_id', $parent_id, $parent_post_type, false, $target_lang );

	// get the child posts of the original parent post
	$child_posts_args = array(
		'post_type'		=>	'child-post',
		'numberofposts'	=>	-1,
		'meta_query'	=>	array(
			array(
				'key'		=>	'_wpcf_belongs_' . $parent_post_type . '_id',
				'value'		=>	$parent_id
			)
		)
	);
	$child_posts = get_posts( $child_posts_args );

	if ( !empty( $child_posts ) ) {

		$create_post_helper = wpml_get_create_post_helper();

		foreach ($child_posts as $child_post) {

			$post_array = array(
				'post_title'		=>	$child_post->post_title,
				'post_author'		=>	$child_post->post_author,
				'post_content'		=>	$child_post->post_content,
				'post_status'		=>	$child_post->post_status,
				'comment_status'	=>	$child_post->comment_status,
				'post_type'			=>	$child_post->post_type
				// add any standard post fields required that are missing
			);

			$child_id = $create_post_helper->insert_post( $post_array, $target_lang, true );

			add_post_meta( $child_id, '_wpcf_belongs_' . $parent_post_type . '_id', $translated_parent_id );

		}
	}

}

Here's the expected workflow.

You have existing parent and child post content in one language.

You want to translate the parent post and have the translated parents have the same child posts as the original..

Note. Because of the way that post relationships are stored, child posts can only have one parent of a given post type. So you cannot have a child post be the child of two different language versions of a parent post. You must create child post copies for the additional languages so that, for example, an English child has an English parent, and the equivalent Spanish parent has a Spanish child (even if you don't actually translate the child and just copy across the English content).

So, you create a translation of your parent post.

You need to save this translation before copying across the content from the original so that the post id of this translation of the parent post is available to the code above.

Now hit the copy content button. That is what triggers the above code.

It will find the child posts of the original language version, and make copies of them in the new language (although the content is not actually translated), and it will assign the parent for these new child posts in the same language.

Try it with a few posts to see how it works and whether it suits your needs, but only after you take a backup of your existing site before making any changes.

Note that the translated child posts are not connected to the originals in any way, and only the standard WordPress fields and the post meta used to store the post relationship are copied, you may need to modify the code to copy additional standard or custom fields.

Let me know how you get on.

#449786

Hey Nigel,
Many thanks - I'll give it a try later this week and I will give you some feedback.

Best,

#449787

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Fingers crossed it suits your needs, but if you need help modifying it then do let me know.

#461049

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

For anyone else reading this Simon pointed out a typo in the above code.

For $childpostargs I had used 'numberofposts' as an argument, which should be either 'numberposts' or 'posts_per_page'.

Thanks for the correction Simon.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.