Home › Forums › Feedback › Using pre_get_posts to filter only posts that include all values › Reply To: Using pre_get_posts to filter only posts that include all values
FYI function should look like this:
// bail early if is in admin
if( is_admin() ) {
return;
}
// bail early if not main query
// – allows custom code / plugins to continue working
if( !$query->is_main_query() ) return;
// get meta query
$meta_query = $query->get(‘meta_query’);
$meta_query = array(
‘relation’ => ‘AND’,
);
foreach ($GLOBALS[‘my_query_filters’] as $key => $name) {
if (!empty($_GET[$name])) {
$values = explode(‘,’, $_GET[$name]);
$meta_query[] = array (
‘key’ => $name,
‘value’ => $values,
‘compare’ => ‘IN’,
);
}
}
$query->set(‘meta_query’, $meta_query);
return;
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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.