콘텐츠로 건너뛰기 사이드바로 건너뛰기

Open

Reported for: Advanced Custom Fields Multilingual 2.1.5

Topic Tags: Compatibility

Overview of the issue

When using Advanced Custom Fields (ACF) with WPML, you might experience issues with conditional logic not functioning correctly in secondary languages. This happens when the conditional logic depends on fields that use object-based or term-based references, such as taxonomy or post object fields.

Workaround

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

  • Add the following code to your theme’s functions.php file.
    /**
     * WPML Workaround for compsupp-wpmltriage-3649 
     */
    add_action( 'init', function() {
    	if ( defined( 'ICL_SITEPRESS_VERSION' ) ) { 
    		add_filter( 'acf/load_field', function ( $field ) {
    
    			if ( empty( $field['conditional_logic'] ) || ! is_array( $field['conditional_logic'] ) ) {
    				return $field;
    			}
    
    			foreach ( $field['conditional_logic'] as $group_index => $rules ) {
    				if ( ! is_array( $rules ) ) {
    					continue;
    				}
    
    				foreach ( $rules as $rule_index => $rule ) {
    					if ( isset( $rule['field'], $rule['value'] ) && is_numeric( $rule['value'] ) ) {
    						// Fetch the parent field
    						$parent_field = acf_get_field( $rule['field'] );
    						if ( ! isset( $parent_field['type'] ) ) {
    							continue;
    						}
    
    						if ( $parent_field['type'] === 'taxonomy' ) {
    							$rule['value'] = apply_filters( 'wpml_object_id', $rule['value'], $parent_field['taxonomy'], true );
    						} elseif ( $parent_field['type'] === 'post_object' ) {
    							$post_types = $parent_field['post_type'] ?? ''; // can be multiple 
    							if ( is_array( $post_types ) ) {
    								foreach ( $post_types as $post_type ) {
    									$translated_id = apply_filters( 'wpml_object_id', $rule['value'], $post_type, true );
    									if ( $translated_id && $translated_id !== $rule['value'] ) {
    										$rule['value'] = $translated_id;
    										break;								
    									}
    								}
    							} else {
    								$rule['value'] = apply_filters( 'wpml_object_id', $rule['value'], $post_types, true );
    							}
    						}
    						$field['conditional_logic'][ $group_index ][ $rule_index ] = $rule;
    					}
    				}
    			}
    
    			return $field;
    		}, 100 );
    	}
    } );
            

답글 남기기

주제에 집중하고 다른 사람을 존중해 주세요. 이 게시물과 관련 없는 문제에 도움이 필요하면 지원 포럼을 사용하여 채팅을 시작하거나 티켓을 제출하세요.

다음 태그를 사용할 수 있습니다:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>