Support

Account

Home Forums ACF PRO Check for a value in a custom query

Solving

Check for a value in a custom query

  • Hi all,
    I have a custom query to show people working is specific divisions (label) in an organisation.
    I want to display the specific division’s name before the cards of the people are displayed.
    Currently I have this wp_query that filters out the division and shows the people in alphabetic order based on their last name. All people are displayed one after the other until the loop ends. What I am looking for is a way to get the division name and show the people, and then the second division and show the people working there etc.
    There are 7 divisions in total.
    My working code so far for the custom wp_query:

    
    $all_query = new WP_Query(array(
    	'post_type'	    => 'collegas',
    	'posts_per_page'    => -1,
    	'meta_query' 		=> array (
    		'relation' 	=> 'AND',
    		'label' 	=> array (
    			'key' 	=> 'medewerker_werkzaam_bij',
    		),
    		'persoon' 	=> array (
    			'key' 	=> 'medewerker_achternaam',
    		),
    	),
    	'orderby'			=> array (
    			'label' 	=> 'ASC',
    			'persoon'	=> 'ASC',
    	)
    ));
    

    As you can see, I am ordering by division (‘label’) and then by lastname (‘persoon’). This works great.
    But how can I check if the name (or value) for the division changes so I can add a heading like <div class=”divisione-heading”><?php ?????? ?></div> before I show the people working there.

    This is the loop:

    
    				if ( $all_query->have_posts() ) {
    					?>
    					<h2 id="opmeer" class="">Iedereen</h2>
    					<div class="<?php echo $column_class; ?>">
    						<?php
    						// Start the loop.
    						while ( $all_query->have_posts() ) {
    							$all_query->the_post();
    							get_template_part( 'loop-templates/content-collegas', get_post_format() );
    						}
    				} else {
    					get_template_part( 'loop-templates/content', 'none' );
    				}
    
  • 
    if ( $all_query->have_posts() ) {
      // add a variable that will treack tha last section shown
      $last_value = '';
      ?>
      <h2 id="opmeer" class="">Iedereen</h2>
      <div class="<?php echo $column_class; ?>">
        <?php
        // Start the loop.
        while ( $all_query->have_posts() ) {
          $all_query->the_post();
          
          // check the value of this against last shown
          if (get_field('your_field_name') != $last_value) {
            // ourput heading for next section
            // your code here
            
            // set last value to this value
            $last_value = get_field('your_field_name');
          }
          
          
          get_template_part( 'loop-templates/content-collegas', get_post_format() );
        }
    } else {
      get_template_part( 'loop-templates/content', 'none' );
    }
    
  • Hi John,

    I’m afraid your solution is not working for me.
    Think I need a ‘foreach’ loop to get this working the way I want.
    This is what I’m looking for:

    
    -- Division Name 1 ---
    Person 1
    Person 2
    Person 3
    
    -- Division Name 2 ---
    Person 1
    Person 2
    
    -- Division Name 3 ---
    Person 1
    Person 2
    Person 3
    Person 4
    Person 5
    
    etc.

    This means that I need to know what value (showing part of the query of my earlier post) is in

    
    'label' 	=> array (
    			'key' 	=> 'medewerker_werkzaam_bij',
    		),
    

    The value should range from 1 to 7. But I really have no idea how to put that value (an ACF field) into a variable so I can check / validate that in a foreach or while loop.

  • What I gave you is the only way to do it. This is the only way to group posts by a custom field value.

    You need to look at the value of the field, you need to compare it with the value of the field on the previous post, if they do not match then you display a new heading.

    To summarize:

    • Set up a variable to store the last value shown
    • Loop over the posts
      • Compare the value of the current post with the last value shown
        • if the values are different
          • Show something based on the new value
          • update the last value show to the new value
  • Hi John,

    I’ll have a look at it again and let you know how it worked out.

  • Hi John,
    I figured out why it was not working.
    In your suggestion:

    
    if (get_field('your_field_name') != $last_value) {
            // ourput heading for next section
            // your code here
            
            // set last value to this value
            $last_value = get_field('your_field_name');
          }
    

    the data you get from get_field('your_field_name') is the data you get from the first item in the database, not the data from the query. In my case it was always ‘3’.
    I rewrote the entire loop, and now it looks like this:

    
    // Get the values from the radio buttons from the ACF object
    $all_names = get_field_object('medewerker_werkzaam_bij');
    // Get the choices from those radiobuttons and put them in an array
    $all_label_names = $all_names['choices'];
    // Count the number of items in the array to use in the 'For' loop. Because a loop starts with '0', decrease the value in the variable with 1				
    $num_labels = count($all_label_names)-1;
    
    for ( $my_label = 0; $my_label <= $num_labels; $my_label++ ){
    $all_query = new WP_Query(array(
    'post_type'			=> 'collegas',
    'posts_per_page'    => -1,
    'meta_query' 		=> array (
    'label' 	=> array (
    'key' 	=> 'medewerker_werkzaam_bij',
    'value' => $my_label,
    ),
    'persoon' 	=> array (
    'key' 	=> 'medewerker_achternaam',
    ),
    ),
    'orderby'			=> array (
    'label' 	=> 'ASC',
    'persoon'	=> 'ASC',
    )
    ));					
    if ( $all_query->have_posts() ) {
    $my_label_name = $all_label_names[$my_label];
    ?>
    <h2 id="<?php echo $my_label_name; ?>" class="<?php echo $my_label_name;?>"><?php echo $my_label_name;?></h2>
    <div class="<?php echo $column_class; ?>">
    <?php	
    	while ( $all_query->have_posts() ) {
    		$all_query->the_post();
    		get_template_part( 'loop-templates/content-collegas', get_post_format() );
    			}
    		} else {
    			get_template_part( 'loop-templates/content', 'none' );
    		}
    			?></div><?php
    		}
    

    And now I have exactly what I was looking for.
    Thanks for replying and moving me in the right direction.

  • Hi John,
    I figured out what went wrong in the first place.
    In your solution:

    
    if (get_field('your_field_name') != $last_value) {
            // ourput heading for next section
            // your code here
            
            // set last value to this value
            $last_value = get_field('your_field_name');
          }
    

    you get the data with get_field('your_field_name') from the first item in the database, in my case this was always 3. So it is not the data from the first item in the query. And that’s why I got stucked.
    After some serious thinking and a lot of trial and error I came up with this solution where I use a For loop to get everything organised by label (division):

    
    // Get the values from the ACF object (radio buttons)
    $all_names = get_field_object('medewerker_werkzaam_bij');
    // Put the choices from that object in an array 
    $all_label_names = $all_names['choices'];
    // Count the number of items to use in the 'For' loop. Because a loop starts with '0', decrease the value for that variable with 1				
    $num_labels = count($all_label_names)-1;
    
    for ( $my_label = 0; $my_label <= $num_labels; $my_label++ ){
    	$all_query = new WP_Query(array(
    		'post_type'			=> 'collegas',
    		'posts_per_page'    => -1,
    		'meta_query' 		=> array (
    									'label' 	=> array (
    										'key' 	=> 'medewerker_werkzaam_bij',
    										'value' => $my_label,
    									),
    									'persoon' 	=> array (
    										'key' 	=> 'medewerker_achternaam',
    									),
    								),
    		'orderby'			=> array (
    									'label' 	=> 'ASC',
    									'persoon'	=> 'ASC',
    		)
    	));					
    	if ( $all_query->have_posts() ) {
    	$my_label_name = $all_label_names[$my_label];
    
    	<h2 id="<?php echo $my_label_name; ?>" class="<?php echo $my_label_name;?>"><?php echo $my_label_name;?></h2>
    	<div class="<?php echo $column_class; ?>">
    		while ( $all_query->have_posts() ) {
    			$all_query->the_post();
    			get_template_part( 'loop-templates/content-collegas', get_post_format() );
    		}
    	} else {
    		get_template_part( 'loop-templates/content', 'none' );
    	}
    	</div>
    }
    
    

    This works for me as I wanted it in the first place.
    But thank you for replying and pushing me in the right place

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

You must be logged in to reply to this topic.