0

Is it possible to construct If/Else queries that look to see if two (or more) Post Object fields are the same – i.e. echo X if Post Object field 1 'value' is the same as Post Object field 2, else echo Y?

To give a practical example, I have a CPT called Matches; the entries containing information from an individual match played by my team. Each Match comprises three ACF field groups: one group for the Line-Up (Roster); one for Goal Scorers and one for Yellow/Red cards.

In each of these groups, I use the Post Object option to relate entries to individual players from my Players CPT. And I'm able to output the Line-Up for each game with the following code from PL1, repeated up to P18 if appropriate:

<li class="team-player">
    <?php 
        $pl1 = get_field('pl1');
        if($pl1) 
    ?>
    <a href="/players/<?php echo $pl1->post_name; ?>/">
        <span class="team-player-number"><?php the_field('sn1'); ?></span>
            <span class="team-player-name">
                <?php echo $pl1->post_title; ?>
            </span>
        </span>
    </a>
<?php endif; ?>
</li>

For each player in the line-up (e.g. PL1), I'd like to construct an If/Else query that connects with the Scorer field group (and then Cards). Essentially, I'd be looking to ask if the Player Post Object from PL1 is the same as the Player Post Object from Scorer 1, echo an image.

What I'm not sure of is how to construct that. I have tried something alone the lines of the following code, but to no avail. I've not found anything from Googling, although I'm conscious that I might not actually have any values to be comparing with what I've attempted:

<li class="team-player">
    <?php 
        $pl1 = get_field('pl1');
        if($pl1) 
    ?>
    <a href="/players/<?php echo $pl1->post_name; ?>/">
        <span class="team-player-number"><?php the_field('sn1'); ?></span>
            <span class="team-player-name">
                <?php echo $pl1->post_title; ?>
            </span>
        </span>
        <span class="team-player-event">
        <?php 
            $s1 = get_field('s1');
            if( $s1 != '30330' ): 
                echo '-';
            elseif ( $s1 = get_field('pll') ):
                echo '<img class="team-event-image" src="/icons/goal.svg">';
            endif;  ?>
        </span>
    </a>
<?php endif; ?>
</li>

At the moment, it is returning '-' for everything. I also have the extra issue of having 11 Scorer fields (11-1 being our record win). So I guess I have to first get this working on a one-on-one basis, before figuring out how to expand it into an array? i.e. if Scorers 1-11 are the same as Player X.

And in a way that isn't inefficient and slow. Can it be done? Am I on the right track?

  • Hi Pete. If $s1 is a post object just as $pl1 is, you could compare against their post ID's? E.g. if ( $s1 && $s1->ID == $pl1->ID ). Or if $s1 is a string of a post ID, then try if ( $s1 == $pl1->ID ). Not sure though, if I understand the question. – Sally CJ Aug 26 '18 at 11:39
  • Hi @SallyCJ. My apologies, I should have qualified my question a little more – because you are spot on. I had wondered and hoped that it was theoretically possible to construct the if/else based on compared post IDs i.e. $s1 post ID equals $pl1 post ID. I'm slightly concerned there isn't efficient way of querying if $s1,$2,$3...$11 post ID(s) equal $pl1 without creating something cumbersome. – Pete Hayman Aug 27 '18 at 21:28
  • Can I see the structure of all the three ACF field groups? I mean, a screenshot of each group. (expanded) And I suppose you're not using Repeatable Fields? – Sally CJ Aug 28 '18 at 2:49
  • 1
    I hope the following s/shots are what you asked for. Let me know if not! So, Players a and Players b; Scorers a and Scorers b; and Cards a and Cards b – Pete Hayman Aug 28 '18 at 20:12
  • 1
    a is a screenshot of the field expanded, while b is a screenshot of the other fields in that group (which are the same format). – Pete Hayman Aug 28 '18 at 20:13
1

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. =)

  • This is excellent. I've made a couple of modifications to the final code in terms of the variables used to suit the page design, but it is spot on in terms of the desired result (if not more than spot on!). Thank you for your help once again @SallyCJ. – Pete Hayman Aug 29 '18 at 14:01
  • I'm glad it worked for you @PeteHayman :) Cheers! – Sally CJ Aug 30 '18 at 1:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.