I’m perplexed at a true/false checkbox that doesn’t seem to give 0 as the false value.
when I echo the value I get 1 (if checked) or I get nothing (if I have checked the box, updated the post and unchecked the box and updated the post). I have added this checkbox field because I am trying to exclude some posts from a WP_Query. I would expect to get a zero value, not an empty value for all these posts. And is there a way to assign a false value in the database to the posts that I have assigned this custom field to.
$values = get_field( 'exclude_project' );
echo "Value: " . $values;
'meta_query' => array(
array(
'key' => 'exclude_project',
'value' => '1',
'compare' => '!='
),
Hey,
This not work for the NULL case.
You can use var_dump(get_field( 'exclude_post' ));
to test.
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => -1,
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'exclude_post',
'value' => '0',
'compare' => '='
),
array(
'key' => 'exclude_post',
'compare' => 'NOT EXISTS'
)
)
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) :
while ( $query->have_posts() ) :
$query->the_post();
the_title();
endwhile;
wp_reset_postdata();
endif; ?>
Unfortunately this did not have any real effect.
$args = array(
'post_type' => 'work',
'post_status' => 'publish',
'posts_per_page' => -1,
'order' => DSC,
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'exclude_project',
'value' => '0',
'compare' => '='
),
array(
'key' => 'exclude_post',
'compare' => 'NOT EXISTS'
)
),
);
I still see all my posts. The ones with a value if 1 are still showing up in my query
$query = new WP_Query( $args );
if ( $query->have_posts() ) :
while ( $query->have_posts() ) : $query->the_post();
get_template_part( 'template-parts/content', 'work' );
endwhile;
wp_reset_postdata();
else:
get_template_part( 'template-parts/content', 'none' );
endif;
Display only selected items:
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => -1,
'post_status' => 'publish',
'order' => 'DSC',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'exclude_post',
'value' => '1',
'compare' => '=',
),
array(
'key' => 'exclude_post',
'compare' => 'EXISTS',
)
)
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) :
while ( $query->have_posts() ) :
$query->the_post();
echo '<br/>';
the_title();
endwhile;
wp_reset_postdata();
endif; ?>
Hide selected items:
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => -1,
'post_status' => 'publish',
'order' => 'DSC',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'exclude_post',
'value' => '0',
'compare' => '=',
),
array(
'key' => 'exclude_post',
'compare' => 'NOT EXISTS',
)
)
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) :
while ( $query->have_posts() ) :
$query->the_post();
echo '<br/>';
the_title();
endwhile;
wp_reset_postdata();
endif; ?>
when I var_dump the variable to see what the unchecked boxes value is it says false or null
The false return is from a post that has had the box checked, updated and then unchecked and updated. the null return is from posts that have not been updated.
Your code is logical but the database is not recording a 0 value for an unchecked box and I can’t go through every post and tick the box and save and then untick the box and save just to get it to reflect some value.
If you have any other ideas I’d love to see them. Thanks for your help!
I had multiple things going on but in the end this was the code that worked for me. Feel free to dissect out of this what you need. In my case the client wanted the featured projects to be at the top and then added the criteria that they would also like to exclude some posts from displaying in the list but still wanted them accessible as single posts. With this my featured posts were at the top and my excluded posts did not display. And if a user clicked both boxes it defaulted to not display.
'post_type' => 'work',
'post_status' => 'publish',
'posts_per_page' => -1,
'meta_key' => 'featured_project',
'orderby' => 'meta_value date',
'order' => 'DSC',
'meta_query' => array(
'relation' => 'OR',
'exclude_posts' => array(
'relation' => 'OR',
array(
'key' => 'exclude_project',
'compare' => '!=',
'value' => '1'
),
array(
'key' => 'exclude_project',
'type' => 'BOOLEAN',
'value' => false
),
array(
'key' => 'exclude_project',
'compare' => 'NOT EXISTS'
),
),
'featured_posts' => array(
'relation' => 'AND',
array(
'key' => 'featured_project',
'value' => '1',
'compare' => '='
),
array(
'key' => 'exclude_project',
'compare' => '!=',
'value' => '1'
),
),
),
);
@kkokoruz Bummer didn’t work for me, must be something on my end.
But now I know how multiple meta queries work, which I need. Need to figure out this issue first.
Thanks.
I know I’m bit late for the party but I think this might be useful to someone. The return value change based on the method you use.
If you use get_posts() then it will return null for false values. If you use WP_Query() you’ll get true if checked and false if not checked.
So here is what worked for me. for get_posts()
'meta_query' => array(
array(
'key' => 'archived_post',
'value' => '1',
'compare' => 'NOT EXISTS'
),)
For WP_Query()
'meta_query' => array(
array(
'key' => 'archive_article',
'value' => '1',
'compare' => '!='
),)
Cheers
Amila
I know I’m bit late for the party but I think this might be useful to someone. The return value change based on the method you use.
If you use get_posts() then it will return null for false values. If you use WP_Query() you’ll get true if checked and false if not checked.
So here is what worked for me. for get_posts()
'meta_query' => array(
array(
'key' => 'archived_post',
'value' => '1',
'compare' => 'NOT EXISTS'
),)
For WP_Query()
'meta_query' => array(
array(
'key' => 'archived_post',
'value' => '1',
'compare' => '!='
),)
Cheers
Amila
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!
We’re reaching out to our multilingual users to ask for help in translating ACF 6.1. Help make sure the latest features are available in your language here: https://t.co/TkEc2Exd6U
— Advanced Custom Fields (@wp_acf) May 22, 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.