Pular para o conteúdo Pular para a barra lateral

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 );
    	}
    } );
            

Deixe uma resposta

Por favor mantenha-se no assunto e seja respeitoso com os outros. Se precisar de ajuda com problemas não relacionados a este post, use nosso Fórum de Suporte para iniciar um chat ou enviar um ticket.

Você pode usar estas tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>