Home › Forums › General Issues › Get posts from checked values (checkbox) does not work
Hi,
I´m trying to get posts from a selected checkbox. I tried the following:
$args = array(
'numberposts' => -1,
'post_type' => 'opdracht',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'niveau_jaren',
'value' => '%Jaar 1%',
'compare' => 'LIKE'
),
array(
'key' => 'field_51c82dd30047c',
'value' => '%Jaar 2%',
'compare' => 'LIKE'
)
)
);
// 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();
This does not show anything. When I try the code for a radio button (from the documentation) it works. So what am I missing here?
Thanks
Your loop code is fine, I’ve tested it using the checkbox field inside a post. What is wrong is your ‘$args’ argument variable.
First off, make sure the post type is correct! Secondly, the meta_key is the FIELD_NAME. make sure that is correct as well, from the looks of it, your second key name was off. Also, value => is wrong as well. don’t use %% unless that’s what the value of your checkbox is. Insert the value of your checkbox.
It would also help if you posted a screenshot of your field group for this particular checkbox field.
<?php $args = array(
'numberposts' => -1,
'post_type' => 'opdracht',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'FIELD_NAME',
'value' => 'CHECKBOX 1 VALUE',
'compare' => 'LIKE'
),
array(
'key' => 'FIELD_NAME',
'value' => 'CHECKBOX 2 VALUE',
'compare' => 'LIKE'
)
)
);
// 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(); ?>
The topic ‘Get posts from checked values (checkbox) does not work’ 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.