Home › Forums › General Issues › WP loop via checkbox value › Reply To: WP loop via checkbox value
You have a checkbox field, by this I’m assuming that this is a True/False field because you say "if it is true then that item is included in the loop"
When you say that you want it included in the loop, here I’m assuming that you mean included in the posts that are returned by WP_Query, either the main query or a custom query.
Is it a custom query or the main WP query that you are looking to alter to only include posts that are marked as true? My answer below is assuming that you mean the main WP query because you did not mention a custom query and "if it is true then that item is included in the loop"
Let me know if I have any of this wrong.
If my assumptions are correct then you need to add a pre_get_posts filter in WP.
To your functions.php file add something like:
// change "my_post_type" to the name of your post type
// change "my_field" to the name of your ACF field
function my_post_type_pre_get_posts($query) {
if (is_admin() || !$query->is_main_query()) {
return;
}
if ($query->query_vars['post_type'] == 'my_post_type') {
$meta_query = array(
array(
'key' => 'my_field',
'value' => '1' // this is the value that ACF saves
// for a true value in a true/false field
)
);
$query->set('meta_query', $meta_query);
}
}
add_action('pre_get_posts', 'my_post_type_pre_get_posts');
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.