ข้ามการนำทาง

Open

Reported for: WPML SEO 2.1.0

Topic Tags: Compatibility

Overview of the issue

When using the Rank Math SEO plugin, the primary category set for a product or post in the default language does not synchronize to its translations. This issue persists even if you set the rank_math_primary_product_cat custom field as copy.

Workaround

Please, make sure of having a full backup of your site before proceeding.

  • Open theme’s functions.php file.
  • Add the following code:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    // compsupp-6580: sync primary term and primary product terms for Rank Math
    add_filter('get_post_metadata', 'compsupp6580_filter_the_post_metadata', 20, 4);
     
    function compsupp6580_filter_the_post_metadata($value, $postId, $key, $single)
    {
     
        $meta_keys_mapping = array(
            'rank_math_primary_category'    => 'category',
            'rank_math_primary_product_cat' => 'product_cat',
        );
     
        if (in_array($key, array_keys($meta_keys_mapping), true)) {
            remove_filter('get_post_metadata', 'compsupp6580_filter_the_post_metadata', 20);
            $value = get_post_meta($postId, $key, true);
            add_filter('get_post_metadata', 'compsupp6580_filter_the_post_metadata', 20, 4);
     
            $args     = [
                'element_id'   => $postId,
                'element_type' => get_post_type($postId),
            ];
            $language = apply_filters('wpml_element_language_code', false, $args);
            $value    = apply_filters('wpml_object_id', $value, $meta_keys_mapping[$key], true, $language);
     
            if (!$single) {
                $value = [$value];
            }
        }
     
        return $value;
    }

ทิ้งคำตอบไว้

โปรดอยู่ในหัวข้อและเคารพผู้อื่น หากคุณต้องการความช่วยเหลือเกี่ยวกับปัญหาที่ไม่เกี่ยวข้องกับโพสต์นี้ โปรดใช้ฟอรัมสนับสนุนของเราเพื่อเริ่มการสนทนาหรือส่งตั๋ว.

คุณสามารถใช้แท็กเหล่านี้:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>