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