Home › Forums › Front-end Issues › Filter posts by ACF value
Hi,
I’m trying to filter the posts by a value. I tried the following:
<?php if (is_page( 'testpagina' ) ) {
// args
$args = array(
'numberposts' => -1,
'post_type' => 'opdracht',
'meta_key' => 'niveau_jaren',
'meta_value' => 'jaar 2'
);
// get results
$the_query = new WP_Query( $args );
// The Loop
?>
<?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();
} ?>
But it does not show anything. The value is correct and there are posts with this value ‘Jaar 2’. Any idea what is wrong? Thanks!
Well.. it does work with a radio button field group. But in my case I need to get it worked with checkboxes. Do I need to change anything?
Also tried this:
<?php if (is_page( 'testpagina' ) ) {
// args
$args = array(
'numberposts' => -1,
'post_type' => 'any',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'niveau_jaren',
'value' => '%Jaar 1%',
'compare' => 'LIKE'
),
array(
'key' => 'niveau_jaren',
'value' => '%Jaar 3%',
'compare' => 'LIKE'
)
)
);
// get results
$the_query = new WP_Query( $args );
This should mean it gets the posts with value ‘Jaar 1’ or ‘Jaar 3’, right? I don’t get anything..
Hi @LeffDesign
Your second lot of code looks like it should work. You have followed the example code from ACF, however, I think the ‘post_type’ => ‘any’ is your issue. I don’t think this is possible. Can you confirm the docs about this?
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!
🚀 This week’s session of ACF Chat Fridays dips into the preliminary results of our first ever user survey. Don’t miss it! https://t.co/3UtvQbDwNm pic.twitter.com/kMwhaJTkZc
— Advanced Custom Fields (@wp_acf) May 9, 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.