Home › Forums › Front-end Issues › Querying single checkbox?
Hi everyone, thanks a lot for such a great plugin! I’ve been testing it for the last few hours with relative success. So far I’ve managed to create a custom field for my posts and this is the resulting code:
define( 'ACF_LITE', true );
include_once('inc/advanced-custom-fields/acf.php');
if(function_exists("register_field_group"))
{
register_field_group(array (
'id' => 'acf_feature-post',
'title' => 'Feature post',
'fields' => array (
array (
'key' => 'field_52312bfec5ee5',
'label' => 'Is this post featured?',
'name' => 'featured_post',
'type' => 'checkbox',
'instructions' => 'If this post is featured, remember to use a 749x375 image',
'choices' => array (
'Featured?' => 'Featured?',
),
'default_value' => 1,
'layout' => 'vertical',
),
),
'location' => array (
array (
array (
'param' => 'post_type',
'operator' => '==',
'value' => 'post',
'order_no' => 0,
'group_no' => 0,
),
),
),
'options' => array (
'position' => 'side',
'layout' => 'default',
'hide_on_screen' => array (
),
),
'menu_order' => 0,
));
}
I have successfully integrated it within my template, but what I’m having trouble is querying the posts that have the “featured” field checked. According to the documentation, I should be doing something like:
$args = array(
'numberposts' => 5,
'post_type' => 'post',
'meta_query' =>
array(
'key' => 'featured_post',
'value' => '1',
'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(); // Restore global post data stomped by the_post(). ?>
But this not only displays every post regardless of the “featured” status, it also seems to ignore the “numberposts” setting and shows 10 posts. What am I doing wrong? Can someone point me in the right direction?
Thanks a lot!
Hi @Cornholio
A few things. I don’t believe you need to use LIKE as per the meta compare.
The true / false field saves either ‘0’ or ‘1’ for the value, so this compare should simply be ‘=’
As for the numberposts, try changing this to posts_per_page
Good luck
E
Hi Elliot, thanks for your answer. Turns out I got it completelky wrong. I was using a normal checkbox without realizing there’s a true/false checkbox that des the job just fine. However, I’m still in a bit of trouble. I have this set up:
if(function_exists("register_field_group"))
{
register_field_group(array (
'id' => 'acf_featured-post',
'title' => 'Featured Post',
'fields' => array (
array (
'key' => 'field_5233c482612a9',
'label' => 'Feature Post?',
'name' => 'featured',
'type' => 'true_false',
'message' => 'Is this a featured post?',
'default_value' => 0,
),
),
'location' => array (
array (
array (
'param' => 'post_type',
'operator' => '==',
'value' => 'post',
'order_no' => 0,
'group_no' => 0,
),
),
),
'options' => array (
'position' => 'side',
'layout' => 'default',
'hide_on_screen' => array (
),
),
'menu_order' => 0,
));
}
And I have no truble calling the featured posts like this:
$posts = get_posts(array(
'meta_query' => array(
array(
'key' => 'featured',
'value' => '1',
'compare' => '=='
)
)
));
echo '<ul>';
if( $posts )
{
foreach( $posts as $post )
{
setup_postdata( $post );
echo '<li>'; the_post_thumbnail('slider'); the_title(); echo '</li>';
}
}
?>
</ul>
<?php wp_reset_postdata(); ?>
However, when I use another loop like this:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h2><a href="<?php the_permalink() ;?>"><?php the_title() ;?></a></h2>
<?php the_post_thumbnail('home'); ?>
<?php the_excerpt(); ?>
<?php endwhile; else: ?>
<p>Sorry, no posts to list</p>
<?php endif; ?>
<?php
global $wp_query;
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $wp_query->max_num_pages
) );
?>
I’m still getting the featured posts. I don’t quite get it, shouldn’t I be getting the rest of the posts?
Thanks a lot!
The topic ‘Querying single checkbox?’ 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.