Home › Forums › General Issues › Querying with meta query on true/false field
I have a true false field (hide_on_public_page)
here is my code in my template:
// The Query
$today = date (‘Ymd’, strtotime(‘-6 hours’));
$future = date (‘Ymd’, strtotime(‘+4 Months’));
$event_query = new WP_Query(
array(
‘post_type’ => ‘al_event’,
‘orderby’ => ‘menu_order’,
‘order’ => ‘ASC’,
‘meta_query’ => array(
array(
‘key’ => ‘event_date_end’,
‘compare’ => ‘>=’,
‘value’ => $today,
),
array(
‘key’ => ‘event_date’,
‘compare’ => ‘<=’,
‘value’ => $future,
),
array(
‘key’ => ‘hide_on_public_page’,
‘value’ => ‘0’,
)
),
) );
This code works, but the problem is that I want to show the fields unless “hid_on_public_page” has been clicked. This seems to only work if checked otherwise there is no value in the field. What would I do to make sure that the posts show up if a user hasn’t clicked that true/false field?
This is how I got it to work, but it seems there should be an easier way?
$event_query = new WP_Query(
array(
'post_type' => 'al_event',
'orderby' => 'menu_order',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'event_date_end',
'compare' => '>=',
'value' => $today,
),
array(
'key' => 'event_date',
'compare' => '<=',
'value' => $future,
),
array(
'relation' => 'OR',
array(
'key' => 'hide_on_public_page',
'value' => '0'
),
array(
'key' => 'hide_on_public_page',
'compare' => 'NOT EXISTS',
)
)
),
) );
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 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.