'sp_outcome', 'numberposts' => -1, 'posts_per_page' => -1, 'meta_key' => 'sp_condition', 'meta_value' => '=', ); $outcomes = get_posts( $args ); foreach ( $meta as $team => $team_results ) { if ( $outcomes ) { $meta[ $team ][ 'outcome' ] = array(); foreach ( $outcomes as $outcome ) { $meta[ $team ][ 'outcome' ][] = $outcome->post_name; } } } } else { reset( $primary_results ); $max = key( $primary_results ); $args = array( 'post_type' => 'sp_outcome', 'numberposts' => -1, 'posts_per_page' => -1, 'meta_key' => 'sp_condition', 'meta_value' => '>', ); $outcomes = get_posts( $args ); if ( $outcomes ) { $meta[ $max ][ 'outcome' ] = array(); foreach ( $outcomes as $outcome ) { $meta[ $max ][ 'outcome' ][] = $outcome->post_name; } } end( $primary_results ); $min = key( $primary_results ); $args = array( 'post_type' => 'sp_outcome', 'numberposts' => -1, 'posts_per_page' => -1, 'meta_key' => 'sp_condition', 'meta_value' => '<', ); $outcomes = get_posts( $args ); if ( $outcomes ) { $meta[ $min ][ 'outcome' ] = array(); foreach ( $outcomes as $outcome ) { $meta[ $min ][ 'outcome' ][] = $outcome->post_name; } } } } // Update results update_post_meta( $post_id, 'sp_results', $meta ); // Return success wp_send_json_success(); } } return new SP_Admin_AJAX();