Skip Navigation

This is the technical support forum for WPML - the multilingual WordPress plugin.

Everyone can read, but only WPML clients can post here. WPML team is replying on the forum 6 days per week, 22 hours per day.

Tagged: 

This topic contains 21 replies, has 2 voices.

Last updated by Nigel 1 month, 4 weeks ago.

Assisted by: Nigel.

Author Posts
January 31, 2024 at 10:15 am #15248420

simonM-37

Hi Support

We are facing the issue on our production site, that duplicates are not always being created correctly after submission from Toolset Forms. The problem is sporadic - happens about 1 in 50 cases, however the problem seems to be happening more often and means the users are not able to use features of our site correctly.

It could be a similar issue to what this user was experiencing:

https://wpml.org/forums/topic/duplicate-doesnt-work-consistently-for-cpts/#post-12261799

I suspect the cause of the error could be that the users are starting the form, but not finishing it, then maybe coming back to it at a later time or something.

I opened a ticket with Toolset,

https://toolset.com/forums/topic/wpml-language-duplicate-not-being-created-correctly-100-of-the-time/page/2/

however I seem to be getting information from the supporter which conflicts with information on the WPML forum, so I would like some confirmation.

Scenario:

- Users create posts of post type "nanny" after submitting a Toolset form.
- We have custom code provided by Toolset, which auto-creates a language duplicate and keeps the language duplicates up to date when users create or update the originals:

function sync_language_duplicates_on_submission( $post_id, $form_data ){
	// Post Form - New Nanny Ad (56261)
	// Post Form - New Job Ad (56251)
	// Post Form - New Native Nanny User (56255)
	// Post Form - New Family User (56244)
	// Post Form - New Message (56276) // Removed - no longer translatable
	// Post Form - Reply Message (56278) // Removed - no longer translatable
	// Post Form - New Nanny Ad - Unverified Nanny (56272)
  $forms = array( 56261, 56251, 56255, 56244, 56272 );
	if ( in_array($form_data['id'],$forms) ){
		global $sitepress;
	// Use WPML API
		do_action( 'wpml_make_post_duplicates', $post_id ) ;
	}
}
add_action( 'cred_save_data', 'sync_language_duplicates_on_submission', 12, 2 );

In the normal case when the code works fine, the language duplicates get the postmeta "icl_lang_duplicate_of", however now the Toolset supporter is telling me that manually created duplicates have to have this postmeta removed.

According to this reply on your forum: https://wpml.org/forums/topic/set-wpml-element-translation-type-from-duplicate-to-translate-api/#post-12895693

the WPML supporter is saying "If you delete _icl_lang_duplicate_of, it would become independent translation".

We never want our language duplicates to be independently translated, rather always "synced" by updating the original. In fact, we prevent the users from even being able to edit the language duplicates themselves.

1) So which is it? Should "synced" language duplicates ALWAYS have _icl_lang_duplicate_of in their metadata or not? If the answer is NO, under what circumstances can language duplicates NOT have _icl_lang_duplicate_of in their metadata?

2) What could be the reason for the posts not being duplicated correctly in the first place? Is our site experiencing the same issue described in the problem https://wpml.org/forums/topic/duplicate-doesnt-work-consistently-for-cpts/#post-12261799 ?

3) If the issue is a "timing" issue, how much time do the users have to create and submit the forms so that the language duplication works properly, so that I can make this clear on the form?

4) Is there any way to "repair" these edge cases in our production site where the auto-duplication didn't occur correctly?

I can happily provide access to the website for more detailed investigation.
Latest examples:
a) Post Type Nanny, original language DE, post ID 61573, no EN duplicate created.
b) Post Type Nanny, original language EN, post ID 61661, no DE duplicate created.

Kind regards
Simon

February 1, 2024 at 9:35 am #15253512

Nigel
Supporter

Timezone: Europe/Madrid (GMT+02:00)

Hi Simon

Before digging into why you are having an intermittent problem with missing duplicates, let me first clarify that duplicates are indeed managed by the _icl_lang_duplicate_of post meta entry.

Different language versions of the same post are managed by a custom WPML table (wp_icl_translations), but what determines if a version of a post in another language is a translation or a duplicate is the post meta entry.

No entry in wp_postmeta with the key _icl_lang_duplicate_of means that posts would be linked translations. The special characteristic of duplicates—which also apply to linked translations—are that when updating the original post the content is copied across to the secondary posts.

When you have successfully created duplicates you should not edit the secondary post. If you click the link for the secondary language it will take you to the WPML translation editor, and upon translating the post it will no longer be a duplicate, and the entry in wp_postmeta will be removed. (If you use the Classic Translation Editor there is a clear warning this will occur; there is no warning when using the Advanced Translation Editor.)

A simple explanation for why posts expected to behave as duplicates don't is that they were at some point, perhaps accidentally, opened in the translation editor.

But from your description it sounds like the duplicates are not made in the first place.

I think you are comfortable looking into the database with phpMyAdmin or similar, yes?

To confirm that there are no other language versions created, search the table wp_icl_translations for element_id = 61573. Note the trid for this row. (If more than one result shows, you want the one with the element_type corresponding to your post type.)

Then search the same table for that trid.

How many entries are there? Just the one with element_id = 61573? In that case that confirms no other language versions of the post were created (whether duplicates or not).

Which means something going wrong with the code used to create the duplicates on the form submission. It's going to be difficult to identify why that might occur around 1 in 50 times. I'll speak to my colleagues and see if they have any ideas.

In the meantime, to fix those cases where the duplicate is missing, the simplest solution—if there are not too many examples—would be to just add the duplicates through the editor when editing the original language version of the post.

Screenshot 2024-02-01 at 09.34.45.png
February 1, 2024 at 10:33 am #15254221

Nigel
Supporter

Timezone: Europe/Madrid (GMT+02:00)

Your code to create the duplicates is fairly straightforward: you hook into the cred_save_data action when the form is submitted, and run the wpml_make_post_duplicates action to generate the duplicates.

In the instances where this is failing it could be because the cred_save_data action is not triggered for some reason, or the WPML code to generate the duplicates fails, and to begin to know why this fails we need to know which it is.

So, can we add a line to log when the cred_save_data hook runs (you'll need your debug.log turned on). Then when the issue occurs again you can check the log for the time the problem post was published to see if the cred_save_data hook ran at all.

So modify your code like so:

function sync_language_duplicates_on_submission( $post_id, $form_data ){
error_log("cred_save_data hook run for post $post_id with form " . $form_data['id']);
    // Post Form - New Nanny Ad (56261)
    // Post Form - New Job Ad (56251)
    // Post Form - New Native Nanny User (56255)
    // Post Form - New Family User (56244)
    // Post Form - New Message (56276) // Removed - no longer translatable
    // Post Form - Reply Message (56278) // Removed - no longer translatable
    // Post Form - New Nanny Ad - Unverified Nanny (56272)
  $forms = array( 56261, 56251, 56255, 56244, 56272 );
    if ( in_array($form_data['id'],$forms) ){
        global $sitepress;
    // Use WPML API
        do_action( 'wpml_make_post_duplicates', $post_id ) ;
    }
}
add_action( 'cred_save_data', 'sync_language_duplicates_on_submission', 12, 2 );

Let's run that and see what it shows.

February 1, 2024 at 3:54 pm #15255804

simonM-37

Hi Nigel

Up front, I'm thankful you are working this ticket!

1) Question - should I close the original related Toolset ticket with Minesh, referencing this one?

2) Thank you for clarifying the difference between duplicate posts and linked translation posts.
>> In that case all our duplicate posts of post type nanny SHOULD in fact have the "_icl_lang_duplicate_of" postmeta as I suspected, right?

3) Our users aren't allowed to update their language duplicate posts, in fact I use a conditional based on "_icl_lang_duplicate_of" to ensure this and a link to redirect them to the original language page so they can edit them.

4) I'm fine with databases, I wish I didn't have to use PHP all the time to do what I could do with my eyes closed on the database! 😉

So in answer to your questions, both queries lead to same results (ie only one entry):

select *
from wp_icl_translations
where element_id = 61573; -- 1221648

select *
from wp_icl_translations
where trid = 1221648;

translation_id	element_type	element_id	trid	language_code	source_language_code
26929	post_nanny	61573	1221648	de	NULL

5) Regarding the manual creation of the duplicates, you're saying I have to click the Duplicate box before clicking the Translate + symbol, right?

6) For those existing edge cases, I think we have maybe 6 or 7 other examples. I may have just clicked the + on them and "translated" them rather than "duplicating" them. I don't expect other data to be associated with nanny posts currently, so would it be safe to revisit those posts in their original languages and check the Duplicate box you mentioned "retroactively", or would it destroy data integrity?

7) Debugging has been activated on production and custom code modified as requested, so hopefully we can catch the next case and get to the source of the error.

Thanks and kind regards
Simon

February 1, 2024 at 4:28 pm #15255930

Nigel
Supporter

Timezone: Europe/Madrid (GMT+02:00)

OK, let's go through each of those in turn:

1) Yes, I think you can close the Toolset ticket, let's handle it here.

2) All the duplicates should have the "_icl_lang_duplicate_of" postmeta entry, yes, otherwise they are not duplicates; if they exist at all, they would be translations and so are not kept in sync.

3) Noted.

4) So, for that example post (ID 61573) there is no other-language version of the post, only the German original. Which means the duplicate wasn't created in the first place, rather than the scenario I speculated on, that a duplicate had been created but it was subsequently edited with the translation editor and un-synced from the original.

5) You don't press the + button. When you check the duplicate checkbox, a "Duplicate" button appears for you to actually create the duplicate (screenshot).

6) If you have some of these problem posts that were missing a duplicate and in the editor you went ahead and created a translation (so the secondary-language version of the post exists, but is not synced to the original as a duplicate), you could insert an entry in wp_postmeta with the "_icl_lang_duplicate_of" key and the original post ID as the meta value, and that would mark it as a duplicate. (You would perhaps then want to update the original language post—maybe make a trivial change, like to punctuation—so that the mechanism to copy across the content from the original to the duplicate was triggered.)

Where there is no translation (the duplicate was never made and you never made a translation), then you can use the UI and the Duplicate button to create a linked duplicate.

7) Let's see what it throws up. It won't necessarily tell us the cause, but will give us something to work with.

Screenshot 2024-02-01 at 16.21.10.png
February 2, 2024 at 8:50 am #15258155
simonM-37

Hi Nigel

1) OK, Toolset ticket closed.

2) OK, thought so, thank you for clarification.

5) Great, thanks for that hint.

6) Inserted wp_postmeta records for the meta_key "_icl_lang_duplicate_of" for the problem posts, and now they are showing correctly as duplicates of their originals and are syncing changes made in the back end correctly to their duplicates. GREAT!

7) Awesome, I will let you know as soon as we come across the next one.

I don't know whether this is related and I'm absolutely happy for this to be handled in a second ticket for clarity, but we also noticed that some duplicate posts are not getting the "Allow Comments" field duplicated correctly in their duplicates even though the meta_key "_icl_lang_duplicate_of" is set correctly. We have the post type set to Allow Comments by default. In fact it is a requirement for those Nannies and Families to be reviewable, but in some cases the Allow Comments is being set to closed instead of open on duplication. Is this a known issue?

As you can see on the screenshot, orignal Nanny post 51809 has wp_posts.comment_status = 'open' but on the duplicate Nanny post 51810 wp_posts.comment_status = 'closed'.

Can it be that there is some kind of time delay when users are creating/editing a Toolset Form? I have a very vague distant memory of something that Waqar once told me in a Toolset ticket, that when a user opens a Toolset Form the draft record is created in the database (cred-auto-draft-XXXXXX) and only when they click Submit that the "final" version of the record (I guess status Published) is then saved to the database. If such a time delay exists, how long is it? Otherwise I can't think of any logical reason why most records save the Allow Comments correctly, but some don't, since the field isn't even exposed anywhere for users to edit.

If you want me to open a second ticket for this, please let me know, or if you open the ticket I can provide more examples/screenshots/context.

Of course I could just update the database to change all comment_status = 'open' to comment_status = 'closed' for all affected post types, but would love to get to the bottom of why this isn't happening automatically.

If you need access to the front end to access the database with Adminer, please let me know. I'm happy to give you credentials.

Thanks and best regards
Simon

New threads created by Nigel and linked to this one are listed below:

https://wpml.org/forums/topic/problem-with-duplicates-not-having-same-comment_status-as-originals/

Screenshot 2024-02-02 at 09.33.37.png
February 2, 2024 at 4:34 pm #15260294

Nigel
Supporter

Timezone: Europe/Madrid (GMT+02:00)

Comments in WordPress are, well, weird, inasmuch as you cannot enable comments for all posts of some type. When registering a custom post type you actually declare support for the ability to enable comments for individual posts.

Comments have to be enabled for each such post, which, as you have seen for yourself, means the column 'comment_status' set to 'open' for the corresponding row in wp_posts.

Regarding where something might go wrong in terms of duplicating a post to other languages, I'd have to first establish how is the comment_status being set to 'open' in the first place, given that the default is 'closed'?

We can continue in this ticket, no problem, but note I am off Monday.

February 6, 2024 at 9:11 am #15269604

simonM-37

Hi Nigel

Oh! I'm surprised to hear that the status is default 'closed'. We had to add the custom code snippet add-shortcode-wpv-post-comments about 21 months back so that the commenting plugin wpDiscuz would work. Could that be the reason?

<?php
/**
 * Shortcode to display the standard WordPress commenting functionality within Toolset Views. Using this in conjunction with wpDiscuz plugin for Review functionality. See also https://toolset.com/forums/topic/using-the-comments-shortcode-within-a-view/
 */

toolset_snippet_security_check() or die( 'Direct access is not allowed' );

// Display post Comments shortcode
add_shortcode('wpv-post-comments', 'my_post_comments');
function my_post_comments() {
	ob_start();
	comments_template();
	$data = ob_get_clean();
	return $data;
}

Kind regards
Simon

February 6, 2024 at 12:06 pm #15271370

Nigel
Supporter

Timezone: Europe/Madrid (GMT+02:00)

It looks like that code has been proposed (by a user, initially) to force displaying the comments UI where it wouldn't ordinarily be displayed.

That includes the output of a View that lists posts (or a single post).

And it includes a single post being displayed by the theme (perhaps with a Toolset content template) where commenting is turned off (comment_status in wp_posts is set to closed).

If commenting is turned on (comment_status = open) then a single post should display the comments UI without the need for any extra code.

The code you shared doesn't seem to be implicated in the problem.

Reviewing the process, I see that actually the default for comment_status is determined by the settings (stored in wp_options, the option_name default_comment_status), and if that is set to open then the newly created posts will have open comments, whether published via the back end or using a front-end Toolset form.

With front-end forms, when the page containing the form is opened and the form rendered, an auto-draft post is created. Then when the form is submitted, that auto-draft post is updated with the form details. The auto-draft itself will have comment_status open if that is the default, and updating the auto-draft with the form content doesn't touch the comment_status.

So it is very hard to see how the comment_status might wrongly be set to closed.

I tried an experiment to see if I could break something, loading the page that renders the form (which generates the auto-draft), and then deleting the auto-draft directly from wp_posts before submitting the form (to simulate something going wrong when someone doesn't submit the form for an extended period).

But the created form still has the comment_status set to open, when that is the default.

How often does this problem occur?

February 7, 2024 at 9:28 am #15275473

simonM-37

Hi Nigel

I seem to remember as we introduced the wpDiscuz plugin that we had to change our comment settings under Settings > Discussion Settings. There we have Default Post Settings set as in the screenshot, in particular "Allow people to submit comments on new posts" is enabled.

Perhaps this is where wp_posts.comment_status is being defaulted to 'open' for new posts? Furthermore, notably under Toolset > Post Type settings, the Comments box needed to be switched on.

I can confirm that on production our default status is set to open:

select * 
from wp_options
where option_name = 'default_comment_status' 

option_id	option_name	option_value	autoload
19	default_comment_status	open	yes

Regarding frequency of occurrence:

select post_type, comment_status, count(*) 
from wp_posts
where post_type in ('nanny','family')
group by post_type, comment_status

post_type	comment_status	count(*)
family	open	542
family	closed	18
nanny	open	3104
nanny	closed	189

So it's getting set somehow to 'closed' for family post type in about 3.3% of the cases, for nanny post type about 6%.

Thanks and best regards
Simon

Screenshot 2024-02-07 at 10.25.14.png
Screenshot 2024-02-07 at 10.02.57.png
February 7, 2024 at 12:36 pm #15276637

Nigel
Supporter

Timezone: Europe/Madrid (GMT+02:00)

Perhaps you answered this already and I missed it, but where the comments are closed, is that on both the original language post and the translations, or only on the translations while the original has comments open?

February 7, 2024 at 1:02 pm #15276685

simonM-37

Hi Nigel

From what I can see via the GUI doing a quick check using a query with Admin Columns, it looks like the originals are all open in the case where the duplicate is closed.

Kind regards
Simon

February 8, 2024 at 11:04 am #15280739

Nigel
Supporter

Timezone: Europe/Madrid (GMT+02:00)

So that sounds like the problem occurs when creating the duplicates with the WPML API hook wpml_make_post_duplicates.

I'd like to update your code that generates the duplicates to catch this when it happens.

Let me work on that and then I'll get back to you, and we can monitor what happens.

February 13, 2024 at 5:12 pm #15299046

Nigel
Supporter

Timezone: Europe/Madrid (GMT+02:00)

Sorry Simon, I haven't forgotten you. I'll get back to you in the morning.

February 14, 2024 at 12:28 pm #15302074

Nigel
Supporter

Timezone: Europe/Madrid (GMT+02:00)

As we've gone down a specific route to work on an issue which is distinct from the original, let's continue this in another thread.

I've split this and shared the proposed code to monitor the issue in the other thread, so that I think we can close here, if agreed.

simonM-37 confirmed that the issue was resolved on 2024-03-12 10:08:28.
This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.