Home › Forums › General Issues › Filtering WP_Query results by select field
Hi,
I’m really close to getting my code to work but can’t figure out this one last thing.
My code outputs a list of recipients grouped by award categories – all good there
But I need to be able to just list the winners and exclude the finalists. The values winner or finalist come from a select field called ‘awarded’
Winner:Winner
Finalist:Finalist
When I include the lines (currently commented out) in order to just see the Winners, all names disappear and I’m just left with the Award Category Headings
$terms = get_terms("category",$args);
$count = count($terms);
if ( $count > 0 ){
foreach ( $terms as $term ) {
echo '<h2>' . $term->name . '</h2>';
echo '<table>';
$loop = new WP_Query( array(
'post_type' => 'recipient',
'post_per_page' => 100,
//'meta_key' => 'awarded',
//'meta_value' => 'Winner',
'meta_key' => 'year',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'id',
'terms' => $term->term_id
)
)
));
// the loop
while ($loop->have_posts()) : $loop->the_post();
You will need to use a meta_query
$loop = new WP_Query( array(
'post_type' => 'recipient',
'post_per_page' => 100,
//'meta_key' => 'awarded',
//'meta_value' => 'Winner',
'meta_key' => 'year',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'id',
'terms' => $term->term_id
)
),
meta_query => array(
array(
'key' => 'awarded',
'value' => 'winner'
)
)
));
Oh thank you so much. You’re a life-saver.
I had tried that, but had missed out the comma after the tax-query.
One more question … please
How would I incorporate an if statement in there so that if there were no posts for the term, it would not print out.
many thanks
in your above code where you have
// the loop
while ($loop->have_posts()) : $loop->the_post();
wrap the while loop in an if
// the loop
if ($loop->have_posts()) :
while ($loop->have_posts()) : $loop->the_post();
// the rest of while loop
endif;
I really appreciate your help John.
Unless I have misunderstood, the above doesn’t work. I feel like the if statement needs be before echoing out the category name, else it will continue to echo out regardless of whether it has posts.
function honour_roll_loop() {
//Args for Categories
$args = array(
'orderby' => 'id',
'order' => 'ASC',
);
echo "<h1>" . get_the_title() . "</h1>";
$terms = get_terms("category",$args);
$count = count($terms);
if ( $count > 0 ){
foreach ( $terms as $term ) {
echo '<h2>' . $term->name . '</h2>';
echo '<table>';
$loop = new WP_Query( array(
'post_type' => 'recipient',
'post_per_page' => 100,
'meta_key' => 'year',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'id',
'terms' => $term->term_id
)
),
meta_query => array(
array(
'key' => 'year',
'value' => '2008'
)
)
));
// the loop
if($loop->have_posts()):
while ($loop->have_posts()) : $loop->the_post();
//Variables
$year = get_post_meta(get_the_ID() , 'year', true); //custom field
$org = get_post_meta(get_the_ID() , 'recipient_organisation', true); //custom field
$awarded = get_field_object('field_564bc3aa81a9f');
// do loop content
echo '<tr>';
echo '<td>' . $year . '</td>';
echo '<td>' . $awarded['value'] . '</td>';
echo '<td>' . get_the_title() . '</td>';
echo '<td>' . $org . '</td>';
echo '</tr>';
endwhile;
endif;
// reset $post so that the rest of the template is in the original context
wp_reset_postdata();
echo '</table>';
}
}
}
Things will need to be rearranged a bit, you just need to move the h2 and the starting table element to after the if statement and move the closing table element to before the end of the if block. You can also move the reset until after your term loop, there isn’t really any reason to reset the post data until after you done looping through the terms.
function honour_roll_loop() {
//Args for Categories
$args = array(
'orderby' => 'id',
'order' => 'ASC',
);
echo "<h1>" . get_the_title() . "</h1>";
$terms = get_terms("category",$args);
$count = count($terms);
if ( $count > 0 ){
foreach ( $terms as $term ) {
$loop = new WP_Query( array(
'post_type' => 'recipient',
'post_per_page' => 100,
'meta_key' => 'year',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'id',
'terms' => $term->term_id
)
),
meta_query => array(
array(
'key' => 'year',
'value' => '2008'
)
)
));
// the loop
if($loop->have_posts()):
echo '<h2>' . $term->name . '</h2>';
echo '<table>';
while ($loop->have_posts()) : $loop->the_post();
//Variables
$year = get_post_meta(get_the_ID() , 'year', true); //custom field
$org = get_post_meta(get_the_ID() , 'recipient_organisation', true); //custom field
$awarded = get_field_object('field_564bc3aa81a9f');
// do loop content
echo '<tr>';
echo '<td>' . $year . '</td>';
echo '<td>' . $awarded['value'] . '</td>';
echo '<td>' . get_the_title() . '</td>';
echo '<td>' . $org . '</td>';
echo '</tr>';
endwhile;
echo '</table>';
endif;
}
// reset $post so that the rest of the template is in the original context
wp_reset_postdata();
}
}
Oh you are too cool my friend! Makes perfect sense. Thanks a billion.
The topic ‘Filtering WP_Query results by select field’ is closed to new replies.
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.