Support

Account

Home Forums Add-ons Repeater Field Check if an entire row (same values) exists in a repeater

Unread

Check if an entire row (same values) exists in a repeater

  • Hello!

    I have two custom post types that share similar (not equal) repeaters; the repeater in the second post type is populated when I save the post in the first post type. This works perfectly.

    My problem is this: every time i update the first post type, new (repeated) rows are added to the second (i’m using add_row() ). I’ve tried several ways to only update the second type’s rows without success. I’ve read all threads about similar problems (check for duplicates, etc) but nothing was quite like this…

    Is there any way to, when saving a post in the first post type, check if the row already exists in the second post type?

    Here’s the code i’m using to save the repeaters (this one is working!):

    add_action('acf/save_post', 'salva_gols', 20);
    
    function salva_gols($post_id){
    
    	if (get_post_type($post_id) != 'jogos') {
    		return;
    	}
    
    	$time_da_casa_gols = get_field('jogos_time_da_casa');
    	$conta_gols_casa = 0;
    	$time_visita_gols = get_field('jogos_time_visitante');
    	$conta_gols_visita = 0;	
    
    	if (have_rows('jogos_gols')) {
    		while(have_rows('jogos_gols')) {
    			the_row();
    
    			$atualiza_atleta = get_sub_field('gols_atleta');
    			$data_jogos = get_field('jogos_data', $post_id);
    			$post_time_casa = get_field('jogos_time_da_casa', $post_id);
    			$time_casa = get_field('times_nome', $post_time_casa->ID);	  
    			$post_time_visita = get_field('jogos_time_visitante', $post_id);
    			$time_visita = get_field('times_nome', $post_time_visita->ID);		  
    			$row_nome_jogo = $time_casa." x ".$time_visita;	  
    			$row_gols_time = get_sub_field('gols_time');  
    			$row_gols_minuto = get_sub_field('gols_minuto'); 
    			$row_gols_momento = get_sub_field('gols_momento'); 
    			$row_gols_minmom = $row_gols_minuto." ".$row_gols_momento;
    
    			$row_gols = array(
    				'data_atletas_gols'	=> $data_jogos,
    				'jogo_atletas_gols'	=> $row_nome_jogo,
    				'time_atletas_gols'	=> $row_gols_time,
    				'momento_atletas_gols'	=> $row_gols_minmom
    			);
    
    			add_row('atletas_gols', $row_gols, $atualiza_atleta->ID);
    
    			if (get_sub_field('gols_time') == $time_da_casa_gols) {
    				$conta_gols_casa++;
    			}
    			if (get_sub_field('gols_time') == $time_visita_gols) {
    				$conta_gols_visita++;
    			}		  
    		}
    	}
    	
    	
    	
    	if (have_rows('jogos_cartoes')) {
    		while(have_rows('jogos_cartoes')) {
    			the_row();
    
    			$atualiza_atleta = get_sub_field('cartoes_atleta');
    			$data_jogos = get_field('jogos_data', $post_id);
    			$post_time_casa = get_field('jogos_time_da_casa', $post_id);
    			$time_casa = get_field('times_nome', $post_time_casa->ID);	  
    			$post_time_visita = get_field('jogos_time_visitante', $post_id);
    			$time_visita = get_field('times_nome', $post_time_visita->ID);		  
    			$row_nome_jogo = $time_casa." x ".$time_visita;	  
    			$row_cartoes_time = get_sub_field('cartoes_time'); 
    			$row_cartoes_tipo = get_sub_field('cartoes_tipo');  
    			$row_cartoes_minuto = get_sub_field('cartoes_minutos'); 
    			$row_cartoes_momento = get_sub_field('cartoes_momento'); 
    			$row_cartoes_minmom = $row_cartoes_minuto." ".$row_cartoes_momento;
    
    			$row_cartoes = array(
    				'data_atletas_cartoes'	=> $data_jogos,
    				'jogo_atletas_cartoes'	=> $row_nome_jogo,
    				'time_atletas_cartoes'	=> $row_cartoes_time,
    				'tipo_atletas_cartoes'	=> $row_cartoes_tipo,
    				'momento_atletas_cartoes'	=> $row_cartoes_minmom
    			);
    
    			add_row('atletas_cartoes', $row_cartoes, $atualiza_atleta->ID);
    		  
    		}
    	}
    	
    
    	update_field( 'gols_-_time_da_casa', $conta_gols_casa, $post_id );
    	update_field( 'gols_-_time_visitante', $conta_gols_visita, $post_id );	
    
    }
Viewing 1 post (of 1 total)

The topic ‘Check if an entire row (same values) exists in a repeater’ is closed to new replies.