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.

Sun Mon Tue Wed Thu Fri Sat
- 8:00 – 14:00 8:00 – 14:00 8:00 – 14:00 8:00 – 14:00 8:00 – 14:00 -
- 15:00 – 17:00 15:00 – 17:00 15:00 – 17:00 15:00 – 17:00 15:00 – 17:00 -

Supporter timezone: Europe/Madrid (GMT+02:00)

This topic contains 33 replies, has 2 voices.

Last updated by davidR-15 1 year, 1 month ago.

Assisted by: Nigel.

Author Posts
April 9, 2024 at 12:15 pm #15499080

Nigel
WPML Supporter since 02/2016

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

I'm making progress on some code to clean up the assignment of terms so that they match the language of the posts, but I just want to check one more thing with you before sharing the solution.

The language of the posts themselves look odd.

Take a look at the screenshot, from a query I ran directly on the database to return only French event posts.

Note how all of these "French" posts have titles with language identifiers in them (for languages other than French).

Are these correctly identified as French posts?

Screenshot 2024-04-09 at 12.42.27.png
April 9, 2024 at 12:26 pm #15499139

davidR-15

Hi Nigel,

Yes, they're tagged correctly. The language code in the parentheses of the title is the language of the tour. As you can see from the post_content, the content of the event itself is indeed in French.
The German versions of those events have the same pattern with the tour guide's spoken language in parentheses and the description indeed being in German.

April 10, 2024 at 2:29 pm #15505364

Nigel
WPML Supporter since 02/2016

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

OK, I have this working on my local copy of your site, and it appears to have fixed the problems with the mnhn-age-group taxonomy, where the terms in different languages were assigned to the wrong language mnhn-event posts.

(That was the original focus of this thread, I haven't checked other taxonomies or other post types.)

The fix involves:

1. running some code one-time to make sure that French mnhn-event posts have French (and only French) mnhn-age-group terms assigned to them. Where they have, say, a German term assigned, the code looks up the French translation of the German term and assigns that instead (removing the German term).
2. running the WPML troubleshooting process to copy the same term assignments from French mnhn-event posts to the other languages.

Before running the code I will provide for 1 it is **essential** that you make a backup in case anything goes wrong. Note that on my local site I disabled WPML plugins before running the code to make sure it didn't intervene in the process in any way.

I recommend you use the plugin Code Snippets, where you can add the below code and save it so that it runs one time only.

Here's the code:

function terms_query( $post_id, $taxonomy ){

    $query = <<<EOT
    SELECT wp_terms.term_id,wp_terms.slug,wp_term_taxonomy.term_taxonomy_id,wp_icl_translations.language_code,wp_icl_translations.source_language_code,wp_icl_translations.trid
    FROM 
        wp_posts
    JOIN 
        wp_term_relationships
    ON
        wp_posts.ID = wp_term_relationships.object_id
    JOIN
        wp_term_taxonomy
    ON 
        wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id
    JOIN
        wp_terms 
    ON
        wp_terms.term_id = wp_term_taxonomy.term_id 
    JOIN 
        wp_icl_translations
    ON
        wp_term_taxonomy.term_taxonomy_id = wp_icl_translations.element_id
    AND
        wp_icl_translations.element_type = CONCAT( 'tax_', wp_term_taxonomy.taxonomy)
    WHERE
        wp_posts.ID = $post_id
    AND
        wp_term_taxonomy.taxonomy = '$taxonomy'
    EOT;

    global $wpdb;

    $results = $wpdb->get_results($query);

	return $results;
}

function posts_query( $post_type ){

    $query = <<<EOT
    SELECT wp_posts.ID
    FROM 
        wp_posts
    JOIN
        wp_icl_translations
    ON
        wp_posts.ID = wp_icl_translations.element_id 
    AND
        wp_icl_translations.element_type = CONCAT('post_', wp_posts.post_type)
    WHERE
        wp_posts.post_type = '$post_type'
    AND
        wp_posts.post_status = 'publish'
    AND 
        wp_icl_translations.language_code = 'fr'
    EOT;

    global $wpdb;

    $results = $wpdb->get_col($query);

	return $results;
}

function french_term( $trid ){

    $query = <<<EOT
    SELECT wp_icl_translations.element_id as term_taxonomy_id, wp_term_taxonomy.term_id as term_id
    FROM
        wp_icl_translations
    JOIN
        wp_term_taxonomy
    ON 
        wp_icl_translations.element_id = wp_term_taxonomy.term_taxonomy_id
    WHERE
        wp_icl_translations.trid = $trid
    AND
        language_code = 'fr'
    EOT;

	global $wpdb;
		
	$results = $wpdb->get_results( $query );

	return $results;
}


$events = posts_query( 'mnhn-event' );

foreach ($events as $event){
	// get the terms assigned to this event
	$age_groups = terms_query( $event, 'mnhn-age-group' );
	
	// iterate over the terms
	foreach ($age_groups as $age_group){
		// is this a French term? If so, skip
		if ( $age_group->language_code == 'fr' ){
			continue;
		}
		// get the French source of the non-French term
		$age_group_fr = french_term( $age_group->trid );
		if ( isset($age_group_fr[0]) ){
			// add the FR term
			$set = wp_set_post_terms( $event, $age_group_fr[0]->term_id, 'mnhn-age-group', true );
		}
		// remove the non-French term regardless
		$remove = wp_remove_object_terms( $event, (int) $age_group->term_id, 'mnhn-age-group' );
	}
}

Run that code snippet and give it a few moments to complete.

Then with WPML active, go to WPML > Support and open the link to the troubleshooting page.

Then apply the "Synchronize post taxonomies" job for MNHN Events as shown in the screenshot.

Again, allow it time for the batch process to run.

That should fix the corrupt entries in your database for the mnhn-age-group taxonomy.

I don't know how you create your content or what your workflows are, but you may want to review those to understand why the problem arises; I don't know if it happens at the moment you create the content, or at some later point due to editing or some other process.

Screenshot 2024-04-10 at 15.18.54.png
April 19, 2024 at 11:37 am #15543327

davidR-15

Thanks Nigel, I will give this a try ASAP.
I will review the import process. However, our code hasn't changed in years, so I assume something must've changed in WPML that causes this.

I've had a number of issues lately with WPML in combination with WordPress CRON that I all can't figure out and that are all not falling under WPML support because… custom code.

The topic ‘[Closed] Trying to get property 'post_type' of non-object in class-wpml-term-actions’ is closed to new replies.