Support

Account

Home Forums General Issues Loading fields: two separate fields, loops not clearing query

Solved

Loading fields: two separate fields, loops not clearing query

  • I have a sports information website that I’m trying to display team records in the edit post screen.

    When a post (custom post type: game) is opened two separate functions are run on two of the text fields: away_record_calc and home_record_calc.

    Here are my functions:

    This one calculates the visitor’s records:

    function get_away_record_update( $field ) {
    
    global $post;
    setup_postdata( $post ); 
    
    	$ID = get_the_ID();
    	$sport = get_the_category(); 
    	$cat = $sport[0]->name; 
    	$away_team = get_field('away_team_2',$ID)->ID;
    	$home_team = get_field('home_team_2',$ID)->ID;
    	$team_record = array('wins' => 0, 'losses' => 0, 'ties' => 0); 
    	$record = '';
    	$record_1 = '';
    $args = array(
    
    	'category_name' => $cat,
    	'meta_key' => 'game_date_2',
    	'orderby' => array( 'game_date_2' => 'ASC','title' => 'ASC' ),
    	'post_type' => 'game',
    	'post_status' => 'publish',
    	'posts_per_page' => 50,
    	'meta_query' => array(
    
    		   	array(
        	 	'relation' => 'OR',
         		
         		array(
            		'key' => 'away_team_2',
            		'value' => $away_team,
            		'compare' => '=',
        			),
    		    array(
            		'key' => 'home_team_2',
            		'value' => $away_team,
            		'compare' => '=',
        			),
        		),     
    	  	),
    	);
    
    $the_query = new WP_Query( $args );
    
    if ( $the_query->have_posts() ) :
    
    while ( $the_query->have_posts() ) : $the_query->the_post();
    
    		if(  get_field('away_team_2')->ID == $away_team ):
        		if( get_field('away_final_2') > get_field('home_final_3')):
        			$team_record['wins']++;
    		  	elseif(get_field('home_final_3') > get_field('away_final_2')):
    				$team_record['losses']++;
    
    		  	elseif( get_field('away_final_2') != null && get_field('home_final_3') != null && get_field('away_final_2') == get_field('home_final_3')):
    				$team_record['ties']++;		  	
    		  	
    		  	else:endif; 
    
    		elseif( get_field('home_team_2')->ID  == $away_team ): 
    
        		if( get_field('home_final_3') > get_field('away_final_2')):
    				$team_record['wins']++;
    			elseif( get_field('away_final_2') > get_field('home_final_3')) :
    				$team_record['losses']++;
    
    		  	elseif( get_field('away_final_2') != null && get_field('home_final_3') != null && get_field('away_final_2') == get_field('home_final_3')):
    				$team_record['ties']++;		  	
    		  	
    		  	else:endif;
      			
      		else:endif;
    
    if($team_record['ties'] > 0):
    	$record_1 = $team_record['wins']."-".$team_record['losses']."-".$team_record['ties'];
    else:
    	$record_1 = $team_record['wins']."-".$team_record['losses'];
    endif;		
     
    
    endwhile;
    
    endif;
    wp_reset_postdata();
    
    	$field['value'] = $record_1;
    
    return $field;
    }
    
    add_filter('acf/load_field/key=field_592d953740343', 'get_away_record_update', 10);
    

    This one calculates the home team’s record:

    
    function get_home_record_update( $field ) {
    
    	$ID = get_the_ID();
    	$sport = get_the_category(); 
    	$cat = $sport[0]->name; 
    	$away_team = get_field('away_team_2',$ID)->ID;
    	$home_team = get_field('home_team_2',$ID)->ID;
    	$team_record = array('wins' => 0, 'losses' => 0, 'ties' => 0); 
    //	$team = strtoupper(get_field('away_team_2',$post)->post_title);
    	$record = '';
    	$record_2 = '';
    
    $args = array(
    
        'category_name' => $cat,
        'meta_key' => 'game_date_2',
        'orderby' => array( 'game_date_2' => 'ASC','title' => 'ASC' ),
        'post_type' => 'game',
        'post_status' => 'publish',
        'posts_per_page' => 50,
    	'meta_query' => array(
    
    			array(
    				'relation' => 'OR',
    			array(
    				'key' => 'away_team_2',
    				'value' => $home_team,
    				'compare' => '=',
    			),
    			array(
    				'key' => 'home_team_2',
    				'value' => $home_team,
    				'compare' => '=',
    			),
    		),     
    	),
    );
    
    $the_query = new WP_Query( $args );
    // The Loop
    if ( $the_query->have_posts() ) :
    
    while ( $the_query->have_posts() ) : $the_query->the_post();
    
    		if(  get_field('away_team_2')->ID == $home_team ):
        		if( get_field('away_final_2') > get_field('home_final_3')):
        			$team_record['wins']++;
    		  	elseif(get_field('home_final_3') > get_field('away_final_2')):
    				$team_record['losses']++;
    
    		  	elseif( get_field('away_final_2') != null && get_field('home_final_3') != null && get_field('away_final_2') == get_field('home_final_3')):
    				$team_record['ties']++;		  	
    		  	
    		  	else:endif;
      
    
    		elseif( get_field('home_team_2')->ID  == $home_team ): 
        		if( get_field('home_final_3') > get_field('away_final_2')):
    				$team_record['wins']++;
    			elseif( get_field('away_final_2') > get_field('home_final_3')) :
    				$team_record['losses']++;
    
    		  	elseif( get_field('away_final_2') != null && get_field('home_final_3') != null && get_field('away_final_2') == get_field('home_final_3')):
    				$team_record['ties']++;		  	
    		  	
    		  	else:endif;
      			
      		else:endif;
    
    if($team_record['ties'] > 0):
    	$record_2 = $team_record['wins']."-".$team_record['losses']."-".$team_record['ties'];
    else:
    	$record_2 = $team_record['wins']."-".$team_record['losses'];
    endif;		
    
    endwhile;
    
    endif;
    
    	$field['value'] = $record_2;
    
    return $field;
    wp_reset_query();
    
    wp_reset_postdata();
    
    }
    
    add_filter('acf/load_field/key=field_592d95d140344', 'get_home_record_update',10);
    

    Independently, each works fine. The loop is correct and it’s pulling in the correct value. But when I include both functions, the loop doesn’t clear and I get the same records (the visitor’s record) for both fields.

    I’ve attached screenshots of my issue.

    Does anyone know what could be happening?

  • I guess problem is with get_the_ID on first function since, as wp said “Retrieve the ID of the current item in the WordPress Loop.”, when mixed you get those values from the current loop item. Try changing both, get_the_ID() into get_the_ID($post->ID) and get_the_category() into get_the_category($post->ID) on your first function “get_away_record_update”.

    pd: nothing to do, but here is 31 may 2017 and i´m answering a post from the future!!! 🙂

  • That didn’t work. It returned the post id of the last post in the query of the get_away_record_update function and used that as the $home_team parameter of the get_home_record_update function. Thanks though!

  • I got this to work by adding $post_id to the get_field() function. And I needed to get the slug from the category object and not the name:

    	$sport = get_the_category(); 
    	$cat = $sport[0]->name; 
    	$away_team = get_field('away_team_2',$post_id)->ID;
    	$home_team = get_field('home_team_2',$post_id)->ID;
    
Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘Loading fields: two separate fields, loops not clearing query’ is closed to new replies.