Support

Account

Forum Replies Created

  • I think there’s an error in andreu’s post and I found 2 sulutions to this case.

    For example, we have a page in English with id 1 and it’s translation to Spanish with id 2. And we have an ACF field group with a rule to match id 1

    Andreu’s first code compares current page ID translated to current language of backend/frontend and the ID. If the user is viewing the translated page (ID=2), the function still returns 2 and the match is 2!=1, but that’s not what we want.

    The first solution is to use WPML function to get ID not for the current language, but for the default language, that the match is set in (note that icl_object_id function is now considered deprecated in WPML):

    global $sitepress;
    //get the default language code
    $default_lang = $sitepress->get_default_language();
    
    /* icl_object is deprecated in WPML, but I leave it here as an example:
    $translated_id = icl_object_id($options['post_id'],'page',true, $default_lang);*/ 
    
    // Proper way to do it now:
    $translated_id = apply_filters( 'wpml_object_id', $options['post_id'], 'page', true, $default_lang);//add defualt lang parameter

    The second and better, IMO, solution is to translate the ID to match instead:

    //WPML support to page rule with ACF 5
    add_filter( 'acf/location/rule_match/page', 'rule_match_post_wpml', 10, 3 );
    function rule_match_post_wpml( $match, $rule, $options ) {
    
    	// vars
    	$post_id = $options['post_id'];
    	$trans_id =  apply_filters( 'wpml_object_id', $rule['value'], 'page', true);
    
    	// validation
    	if( !$post_id ) {
    		return false;
    	}
    
    	// compare
    	if( $rule['operator'] == "==") {
    
    		$match = ( $post_id == $trans_id );  
    
    	} elseif( $rule['operator'] == "!=") {
    
    		$match = ( $post_id != $trans_id );  
    
    	}
    
    	// return
    	return $match;
    }
  • John, your solution is right and worked for me, but there is a typo in the for(…) parameters. Since this post is the first to come up in Google for the issue I hope this helps some people:

    $repeater_value = get_post_meta($post_id, 'repeater_field_name', true);
    if ($repeater_value) {
      for ($i=0; $i<$repeater_value;/*<-fixed*/ $i++) {
        $meta_key = 'repeater_field_name_'.$i.'_sub_field_name';
        $sub_field_value = get_post_meta($post_id, $meta_key, true);
      }
    }
Viewing 2 posts - 1 through 2 (of 2 total)