Home › Forums › General Issues › Query in URL for ACF chexbox field in CPT
Hello,
I have a CPT and an ACF checkbox field called “coup_de_coeur”.
I’d like to display all the CPT posts when this checkbox is checked by the URL, like :
mywebsite.com/my-cpt?coup_de_coeur=VALUE (only one value is possible anyway)
I tried with this bit of code (I’m no expert, I checked several topics) but it doesn’t work, only get me a blank / error page.
Does someone has an idea please? 🙂
add_action('pre_get_posts', 'my_pre_get_posts');
function my_pre_get_posts( $query ) {
$meta_query = $query->get('meta_query');
if( isset($_GET['coup_de_coeur']) )
{
$meta_query[] = array(
'key' => 'coup_de_coeur',
'value' => $_GET['coup_de_coeur'],
'compare' => '=',
);
}
$query->set('meta_query', $meta_query);
return;
}
A checkbox stores as a serialised array, change the compare from = to LIKE
That I believe should do it
Wow, thank you for your quick answer jarvis!
I changed the “=” to LIKE but it still doesn’t work unfortunately, still critical error.
For information, I placed this function in the functions.php of my child theme
Critical error would be something else, not the = or LIKE
What does the critical error say? Does it provide a line of code? If so, what line and what code is nearby?
Yes, if I remove the function below I don’t have any error :
add_action('pre_get_posts', 'my_pre_get_posts');
function my_pre_get_posts( $query ) {
$meta_query = $query->get('meta_query');
if( isset($_GET['coup_de_coeur']) )
{
$meta_query[] = array(
'key' => 'coup_de_coeur',
'value' => $_GET['coup_de_coeur'],
'compare' => 'LIKE',
);
}
$query->set('meta_query', $meta_query);
return;
}
I checked and it seems like everything is correctly closed, no comma missing…
Unfortunatelu the critical error doesn’t provide any line of code so I’m a bit in the dark…
I tried to put WP Debug to true but no luck either.
Can you comment out or remove the return at the bottom. See if that solves it.
It all looks OK.
Thank you for your inputs, unfortunately it’s not working either…
I also tried to change the function’s name, no luck.
If I comment add_action('pre_get_posts', 'my_pre_get_posts');
it’s working, but it doesn’t use the function ;;
Hmm
Right after the function line, try something like
if ( is_admin() || !$query->is_main_query() )
return $query;
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.
Thanks a lot @jarvis for your time and patience, I really appreciate it!
Your code is working perfectly, and no more critical error for me! I’m very happy to continue building my website now 🙂
My menu disappeared though, but I found a solution here by using a $query->is_main_query()
if anyone has the same problem.
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.