最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - How to assign multiple terms in different languages to a post in WordPress (WPML & Formiddle)? - Stack Overflow

programmeradmin0浏览0评论

How to assign multiple terms in different languages to a post in WordPress with WPML and Formidable Forms?

I am using WordPress + WPML + Formidable Forms and programmatically assigning terms in the job_listing_category taxonomy to a post using wp_set_object_terms(). The terms are added after the post is created using the frm_after_create_entry hook.

Setup:

  • The website has two languages: Georgian (ka-ge) and Russian (ru).
  • Job listings are created using Formidable Forms.
  • The frm_after_create_entry hook is used to assign terms after post creation.
  • If a listing is created in English, only English terms are assigned.
  • If a listing is edited on the Georgian site (not in the admin panel) and saved, Georgian terms are also added automatically.
  • However, doing the same programmatically does not work – WPML filters the terms.

What I am doing?

This code runs inside the frm_after_create_entry hook:

add_action('frm_after_create_entry', function ($entry_id, $form_id) {
    if ($form_id != 18) { // Form ID
        return;
    }

    $entry = FrmEntry::getOne($entry_id);
    if (!$entry->post_id) {
        return;
    }

    $post_id = $entry->post_id;

    // Get current post terms
    $current_terms = wp_get_post_terms($post_id, 'job_listing_category', ['fields' => 'ids']);

    // Get translated term IDs
    $translated_term_id_en = apply_filters('wpml_object_id', $current_terms[0], 'job_listing_category', false, 'en');
    $translated_term_id_ge = apply_filters('wpml_object_id', $current_terms[0], 'job_listing_category', false, 'ka-ge');

    // Set terms
    $result = wp_set_object_terms($post_id, [$translated_term_id_en, $translated_term_id_ge], 'job_listing_category');

    error_log(print_r([$translated_term_id_en, $translated_term_id_ge], true)); // Expected [322, 358]
    error_log(print_r($result, true)); // Actually getting [322, 322]

}, 10, 2);

Expected result:

  • The post should have both terms:
  • One in English (translated_term_id_en).
  • One in Georgian (translated_term_id_ge).
  • WPML should not override terms when switching languages. Actual result:
  • $translated_term_id_en = 322, $translated_term_id_ge = 358.
  • After wp_set_object_terms(), WPML replaces the second term with the equivalent of the current post language, resulting in [322, 322].

What I have tried?

  1. Disabling WPML term filtering before assigning terms: remove_filter('get_the_terms', 'wpml_get_object_terms_filter', 10); WPML still replaces terms after switching languages.

  2. Manually assigning terms through form editing – works correctly, but the same process does not work programmatically.

Question: How can I correctly assign multiple terms in different languages to a post in WordPress + WPML inside the frm_after_create_entry hook, so that WPML does not override them when switching languages?

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论