Home › Forums › Front-end Issues › Can't figure out how to make query checkbox filter
Hello,
I’m stuck on the following: I’d like to create a filter with the checkbox which I’m using as “category”. I named it ‘trainingscategory’. With this I’d like to filter the posts from my CPT ‘training’. I don’t suppose I’m the first one trying this but I have a hard time finding answers or perhaps I just don’t understand them. I’ve read the query and filter by custom field posts a dozen times but I can’string them together.
In my functions.php I’ve set:
<?php
function my_pre_get_posts( $query ) {
// do not modify queries in the admin
if( is_admin() ) {
return $query;
}
// only modify queries for 'training' post type
if( isset($query->query_vars['post_type']) && $query->query_vars['post_type'] == 'training' ) {
// allow the url to alter the query
if( isset($_GET['trainingscategory']) ) {
$query->set('meta_key', 'trainingscategory');
$query->set('meta_value', $_GET['trainingscategory']);
}
}
// return
return $query;
}
add_action('pre_get_posts', 'my_pre_get_posts');
For now on my page I’m only showing a table with the posts in CPT ‘training’ without the filtering.
For my sidebar I’d like to show the values set in ‘trainingscategory’.
<?php
// Load field settings and values.
$field = get_field_object('trainingscategory');
$cats = $field['value'];
// Display labels.
if( $cats ): ?>
<ul>
<?php foreach( $cats as $cat ): ?>
<li><?php echo $field['choices'][ $cat ]; ?></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
This doesn’t show anything since this only shows what is set on the current page if I understood correctly. However I couldn’t figure out how to show all values in a list.
For the filtering I think I’d need to set the above values in a add_query in the URL?
But then how do I make the table show only the posts with that value?
I think it should be something like below but I can’t figur out how to make this work as filter result. (I’m using a ul here instead of my table to save space)
// Query
$the_query = new WP_Query(array(
'post_type' => 'training',
'posts_per_page' => -1,
'meta_key' => 'trainingscategory',
'orderby' => 'meta_value',
'order' => 'DESC'
));
?>
<?php if( $the_query->have_posts() ): ?>
<ul>
<?php while( $the_query->have_posts() ) : $the_query->the_post();?>
<li>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
<?php wp_reset_query(); // Restore global post data stomped by the_post(). ?>
I have a feeling I’ve read the answer a dozen times in the posts but I simply can’t figure it out. If someone could help me in the right direction I’d appreciate that alot.
Kind regards,
You must be logged in to reply to this topic.
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!
🤔 Curious about the ACF user experience? So are we! Help guide the evolution of ACF by taking part in our first ever Annual Survey and guarantee you’re represented in the results. https://t.co/0cgr9ZFOJ5
— Advanced Custom Fields (@wp_acf) May 8, 2023
© 2023 Advanced Custom Fields.
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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.