WPML
ステータス
未解決
トピックタグ
Bug

問題の概要

投稿にタームを割り当てると、特定のタームが割り当てられている投稿の累計数がWordPressに保存されます。

ある投稿からタームを削除すると、カウントは減少します。設定でタームを同期するようになっている場合、翻訳されたタームは翻訳された投稿から削除されますが、カウントは減りません。

回避策

以下のコードスニペットを(プラグイン経由、またはテーマのfunctions.phpファイルに)追加することで、翻訳のタームのカウントを強制的に再計算できます。

add_action( 'deleted_term_relationships', function($object_id, $tt_ids, $taxonomy){

    $trids = array();
    $translated_tt_ids = array();

    foreach ($tt_ids as $tt_id) {
        $trids[] = apply_filters( 'wpml_element_trid', NULL, $tt_id, 'tax_'.$taxonomy );
    }

    foreach ($trids as $trid) {
        $translations = apply_filters( 'wpml_get_element_translations', NULL, $trid, 'tax_'.$taxonomy );
        foreach ($translations as $lang => $translation) {
            $translated_tt_ids[] = $translation->element_id;
        }
    }    

    $translated_tt_ids = array_diff( $translated_tt_ids, $tt_ids );

    if ( count( $translated_tt_ids ) ) {
        wp_update_term_count( $translated_tt_ids, $taxonomy );
    }

}, 11, 3);

すべての既知の問題 →