Support

Account

Home Forums ACF PRO check if a certain value exists within query result

Solving

check if a certain value exists within query result

  • Hello, I have a taxonomy field called “saison” which can return multiple values (all 12 months of the year). For instance I managed to call them and display them in a listview:

    <?php $terms = get_field('saison');
    if( $terms ): ?>
    	<ul>
    	<?php foreach( $terms as $term ): ?>
    		<span><?php echo $term->name; ?></span>
    	<?php endforeach; ?>
    	</ul>
    <?php endif; ?>

    It’s giving me something like “April Mai Juni Juli” (not separated by commas).

    Instead of having it in a listview, I would like to check if a certain month exists in the output. If so, I would like to display div#A and if not display div#B. Any ideas on how to achieve that? Thank you very much!

  • To display different things if the term is not set in the field the first thing you would need to do is get all of the terms https://developer.wordpress.org/reference/functions/get_terms/

    Then you would loop over all of the terms and see if each term is set in the field to see what you would display.

  • Hm. The above code returns an array of all terms. I also tried out get_field_object which extends the array returning an array of all fields, labels, terms etc. I can’t see how the get_terms function would give a different result?

    However how would I look over all terms?

    I tried to compare using PHP in_array function but that didn’t work (it’s always giving me “true” no matter if the term is set or not).

    Any idea on how to write that loop?
    Thank you, Ralf

  • get_field_object() used for a taxonomy field will not return the choices. get_field() only returns the selected choices. The only way that you can get a list of all terms to show the unsollected ones is to use get_terms()

    
    $args = array(
      'taxonomy' => 'YOUR TAXONOMY HERE'
    );
    $terms = get_terms($args);
    $selected_terms = get_field('saison', false, false); // get only term IDs
    if ($selected_terms) {
      foreach ($terms as $term) {
        if (in_array($term->term_id), $selected_terms) {
          // this one is selected
        } else {
          // term is not selected
        }
      }
    }
    
  • Thank you, getting closer. I had a parse error with the code you provided and changed it slightly to that:

    <?php
    $args = array(
      'taxonomy' => 'reisezeit'
    );
    $terms = get_terms($args);
    $selected_terms = get_field('saison', false, false); // get only term IDs
    if ($selected_terms) {
      foreach ($terms as $term) {
        if (in_array($term->term_id, $selected_terms)) {
          echo 'Found';
        } 
        else {
          echo 'NotFound';
        }
      }
    }
    ?>

    This returns me: FoundFoundNotFoundNotFoundFoundFoundFoundFoundFoundFoundFoundFoundFoundFoundNotFound
    which is a little bit strange since it’s 15 matches/non matches, but my taxonomy can have 16 values (12 months + 4 seasons). Querying like so:

    <?php
    $args = array(
      'taxonomy' => 'reisezeit'
    );
    $terms = get_terms($args);
    $selected_terms = get_field('saison', false, false);
    print_r ($selected_terms);
    ?>

    Gives me the ID’s of the selected terms:
    Array ( [0] => 105 [1] => 95 [2] => 96 [3] => 97 [4] => 106 [5] => 98 [6] => 99 [7] => 100 [8] => 101 [9] => 107 [10] => 102 [11] => 103 )
    which should be fine.

    But how can I query in this result for a specific term ID? (as an end result I want to have 12 divs for all 12 months, but those months that are not selected greyed out // the selected ones marked with a color). So I need to do 12 in_array queries.

    Thank you, Ralf

Viewing 5 posts - 1 through 5 (of 5 total)

The topic ‘check if a certain value exists within query result’ is closed to new replies.