Custom Query: If One Post Object Field Value Is The Same As Another

Like I’ve pointed in my comment, you can find out if a ‘player’ is a ‘goal scorer’ and/or a ‘yellow/red card holder’, by comparing against their post ID.

So let’s say there’s a ‘match’ with the following:

  • 9 ‘players’ (ACF fields: pl1 to pl9)

  • 3 ‘goal scorers’ (ACF fields: s1 to s3)

  • 1 ‘card’ (ACF field: y1)

Now, for comparing a single ‘player’ against a single ‘goal scorer’ or ‘card holder’, you can do it like so:

$pl1 = get_field( 'pl1' ); // PL1; player
$s1 = get_field( 's1' );   // S1; goal scorer
$y1 = get_field( 'y1' );   // Y1; card holder

// Check if S1 == PL1
if ( $pl1 && $s1 && $s1->ID == $pl1->ID ) {
    echo 'S1 equals PL1<br>';
}

// Check if Y1 == PL1
if ( $pl1 && $y1 && $y1->ID == $pl1->ID ) {
    echo 'Y1 equals PL1<br>';
}

But for comparing a single ‘player’ against all available ‘goal scorers’ and/or ‘card holders’, you can utilize the get_fields() function like this:

$pl1 = get_field( 'pl1' ); // PL1; player
$fields = get_fields( false, false );

$total_goals = 0;
$total_cards = 0;
$pl1_cards = 0;
$pl1_goals = 0;

foreach ( $fields as $key => $post_id ) {
    // Check if the field name is 's{n}' where {n} is a number. If yes,
    // then it's a 'goal scorer' field. (based on your naming style)
    if ( preg_match( '/^s(\d+)$/', $key ) ) {
        // Check if PL1 == S{n}
        if ( $pl1 && $post_id == $pl1->ID ) {
            echo strtoupper( $key ) . ' equals PL1<br>';
            $pl1_goals++;
        }

        $total_goals += $post_id >= 1 ? 1 : 0;
    }

    // Check if the field name is 'y{n}' where {n} is a number. If yes,
    // then it's a 'card holder' field. (based on your naming style)
    if ( preg_match( '/^y(\d+)$/', $key ) ) {
        // Check if PL1 == Y{n}
        if ( $pl1 && $post_id == $pl1->ID ) {
            echo strtoupper( $key ) . ' equals PL1<br>';
            $pl1_cards++;
        }

        $total_cards += $post_id >= 1 ? 1 : 0;
    }
}

echo '<p>' .
    'This match had ' . $total_goals . ' goals and ' . $total_cards . ' cards.<br>' .
    'Player "PL1" had ' . $pl1_goals . ' goals and ' . $pl1_cards . ' cards.<br>' .
'</p>';

In the $fields = get_fields( false, false );, if you set the second parameter to true (which is the default value), then you’d rename the $post_id to $post_obj, and use $post_obj->ID == $pl1->ID and $post_obj->ID >= 1.

Also, these variables are for demo purposes: $total_goals, $pl1_goals, $total_cards, and $pl1_cards. So you can ignore/remove them. However, it might be helpful to mention that if the $total_goals equals to the $pl1_goals, then you can say that all goals were scored by PL1.

Hope this answer helps you, and let me know if you need further assistance, or if perhaps I misunderstood anything. =)