Home › Forums › ACF PRO › Check for a value in a custom query › Reply To: Check for a value in a custom query
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.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.