Home › Forums › General Issues › check box query
hello friends
I made a checkbox field and it has 20 choice (
Facilities hotel)
I want to display facilities to my website but some hotele have 2 facilities and another one have 10. so I use query checkbox but it doesn’t work !
look this is my html :
<div class=”facilities”>
<div class=”livicon” data-name=”medal” data-size=”25″ data-color=”#fff” ></div>
title
</div>
I made check box choice like :
<div class=”livicon” data-name=”medal” data-size=”25″ data-color=”#fff” ></div>
title : TITLE
and use this query:(‘facilities’ name of custom field)
<?php
$posts = get_posts(array(
‘meta_query’ => array(
array(
‘key’ => ‘facilities’, // name of custom field
‘value’ => ‘”red”‘, // matches exactly “red”
‘compare’ => ‘LIKE’
)
)
));
<div class=”facilities”>
if( $posts ) {
<?php the_field(‘facilities’); ?>
}
</div>
?>
what’s the problem ? thanks
Hi @saberap
I’m afraid I don’t understand your goal. Could you please let me know the result you want?
Also, it seems there are some syntax errors in your code. First, you forgot to add the PHP closing and opening tag before and after the div element. Second, kindly avoid using $posts
variable as it’s sometimes used by WordPress. Third, you need to loop through the returned posts list to show the checkbox value for each post. And lastly, you need to pass the post ID because the code is executed outside of The Loop.
So, it should be like this:
<?php
// use another variable than "$posts"
$the_posts = get_posts(array(
'meta_query' => array(
array(
'key' => 'facilities', // name of custom field
'value' => '"red"', // matches exactly "red"
'compare' => 'LIKE'
)
)
));
// don't forget the PHP closing tag
?>
<div class="facilities">
<?php
// and also the PHP opening tag
if( $the_posts ) {
// loop through the posts
for( $the_posts as $the_post ){
// show the value
the_field('facilities', $the_post->ID );
}
}
?>
</div>
I hope this makes sense 🙂
The topic ‘check box query’ 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.