Home › Forums › General Issues › Query in URL for ACF chexbox field in CPT › Reply To: Query in URL for ACF chexbox field in CPT
Hi @daisuke
Here you go, this code has been tried and tested and confirmed working:
add_action('pre_get_posts', 'filter_posts_by_acf');
function filter_posts_by_acf( $query ) {
if(is_admin()){
return;
}
$meta_query = $query->get('meta_query');
if( isset($_GET['coup_de_coeur']) ){
$meta_query = [];
$meta_query[] = array(
'key' => 'coup_de_coeur',
'value' => $_GET['coup_de_coeur'],
'compare' => 'LIKE',
);
}
$query->set('meta_query', $meta_query);
return $query;
}
Adding your code initially, I got an error but its because I already had a function with the same name, so changed it.
I then added some validation and a quick tidy.
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.