Reece
Reece

Reputation: 2701

How filter custom posts by Advanced Custom Fields checkbox

I am creating a property site where there is a featured property on the home page. To define a property as featured I have created an acf checkbox with the value as Yes when checked. I have tried filtering the posts by checking if the checkbox is checked but I cannot figure it out. Here's my code which isn't working;

<?php 
    $args = array(
        'post_type'         => 'property',
        'posts_per_page'    => 1,
        'meta_key'          => 'featured_property',
        'meta_value'        => 'Yes'
    );

    $query = new WP_Query( $args );
?>

<?php if( $query->have_posts() ) : ?>
    <?php  
        $main_field = get_field('images');
        $first_row = $main_field[0];
        $img = $first_row['image'];
        $img_crop = $img['sizes']['fresh_size'];
    ?>

    <img src="<?php echo $img_crop; ?>" alt="featuredproperty" class="img-fluid">
    <?php wp_reset_postdata(); ?>
<?php endif; ?>

enter image description here

READ THIS: for anyone attempting to do this with a checkbox like I was don't. after a little research i found out "Checkboxes are stored as serialized data and you’re not going to be able to use WP_Query to filter by a checkbox field" Use true / false instead and check if the value equals '1' or '2' depending on what you are trying to achieve.

https://support.advancedcustomfields.com/forums/topic/using-checkbox-fields-in-custom-queries/

Upvotes: 1

Views: 3799

Answers (2)

danjah
danjah

Reputation: 1246

Remove this part:

'meta_key'          => 'featured_property',
'meta_value'        => 'Yes'

Instead, filter out who has the checkbox checked inside the loop. You are also missing parts of the loop. Try this code:

    <?php if( $query->have_posts() ) : ?>
        (...)

        <!-- start of the loop -->
        <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
              <?php if( get_field('featured_property') ) { // << FROM HERE ?>
                  <img src="<?php echo $img_crop; ?>" alt="featuredproperty" class="img-fluid">
              <?php } // << TO HERE ?>
        <?php endwhile; ?><!-- end of the loop -->

        <?php wp_reset_postdata(); ?>
    <?php endif; ?>

I have cut out the first part of your code to make it easier to read.

--

Or, if you would like to use meta_key instead, try adding:

'compare' => 'EXISTS'

Upvotes: 2

Pavel
Pavel

Reputation: 310

<?php 
$args = array(
    'post_type'         => 'property',
    'posts_per_page'    => 1,
    'meta_key'          => 'featured_property',
    'meta_value'        => 'Yes'
);

$query = new WP_Query( $args );
?>

<?php if( $query->have_posts() ): ?>
<ul>
<?php while( $query->have_posts() ) : $query->the_post();
   $images = get_field('images');
    $first_row = $main_field[0];
    $img = $first_row['image'];
    $img_crop = $img['sizes']['fresh_size'];
?>
    <img src="<?php echo $img_crop; ?>" alt="featuredproperty" class="img-fluid">
<?php endwhile; ?>
</ul>
<?php endif; ?>

<?php wp_reset_query();  // Restore global post data stomped by the_post(). 
?>

Please check your settings under acf checkbox "Choices", check if it "Yes : Yes" or "yes : Yes" and fix your 'meta_value' if you have "yes : Yes" to 'meta_value' => 'yes'. Checkbox saves data as value-label. I think you have issue with your configuration of checkbox.

What type of 'images' field do you use? Is it repeater or gallery? If you using gallery then for src of the image you need to use: $images = get_field('images'); $img_crop = $images[0]['sizes']['fresh_size'];

Upvotes: 0

Related Questions