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).
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.
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.
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.
Manage Cookie Consent
We use cookies to optimize our website and services. Your consent allows us to process data such as browsing behavior. Not consenting may affect some features.
Functional
Always active
Required for our website to operate and communicate correctly.
Preferences
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
Statistics
We use these to analyze the statistics of our site. Collected information is completely anonymous.The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
Marketing
These cookies track your browsing to provide ads relevant to you.