Home › Forums › General Issues › GET (url) parameters for data arrays?
Hi there, loving this plugin. I’ve purchased a few add-ons and they’ve been worth every penny!
Ok, here’s my question:
I have followed these instructions and can now successfully make queries using parameters appended to the end of my urls.
However, so far I have been querying radio button fields meaning the custom fields only contain a single value. However, when trying to query checkbox fields (containing multiple values) it doesn’t return any results.
Here is a hypothetical example: I have a field named music-taste. Options include “hip hop”, “blues”, “punk”.
I want to be able to make a query using a parameter similar to this:
?music-taste=blues
However, currently this isn’t working. Like I say, I have got radio buttons working fine.
Any help much appreciated 🙂
This document will probably help you http://www.advancedcustomfields.com/resources/querying-relationship-fields/
Chackbox fields are stored as arrays similar to the way relationship fields are stored.
So your meta query would look something like this.
meta_query' => array(
array(
'key' => 'music-taste', // name of custom field
'value' => '"' . $taste . '"', // matches exaclty "123", not just 123. This prevents a match for "1234"
'compare' => 'LIKE'
)
)
Hi John, thanks for this, I didn’t know they were treated similar to a relationship. Here is my current functions.php code based on Elliot’s tutorial:
// array of filters (field key => field name)
$GLOBALS['my_query_filters'] = array(
'example_field' => 'example1',
'example_field_two' => 'example2',
//'music-taste' => 'music-taste' (this is what I'm trying to make work)
);
// action
add_action('pre_get_posts', 'my_pre_get_posts', 10, 1);
function my_pre_get_posts( $query ) {
// bail early if is in admin
if( is_admin() ) {
return;
}
// get meta query
$meta_query = $query->get('meta_query');
// loop over filters
foreach( $GLOBALS['my_query_filters'] as $key => $name ) {
// continue if not found in url
if( empty($_GET[ $name ]) ) {
continue;
}
// get the value for this filter
// eg: http://www.website.com/events?city=melbourne,sydney
$value = explode(',', $_GET[ $name ]);
// append meta query
$meta_query[] = array(
'key' => $name,
'value' => $value,
'compare' => 'IN',
);
}
// update meta query
$query->set('meta_query', $meta_query);
}
I’m unsure of how to incorporate your suggested code, being as my working queries are ‘compare’ => ‘IN’, and my checkboxes need to be ‘compare’ => ‘LIKE’.
Many thanks!
There are a couple of things here, the first is the LIKE
part. Another is that you can have multiple values, so you need to add a separate meta query for and then because of the way you’re doing this you may need to use a nested query with a relationship set, for more info on nested meta queries https://make.wordpress.org/core/2014/10/20/update-on-query-improvements-in-4-1/
I’m pretty sure this is correct, but I haven’t tested it. This will only work with values that are stored as arrays, like checkboxes. If you have other types of values that can be submitted in the url then you’d need to do a switch on $name
and construct each nested query to match the way the type of is stored.
function my_pre_get_posts( $query ) {
// bail early if is in admin
if (is_admin()) {
return;
}
// get meta query
$meta_query = $query->get('meta_query');
// loop over filters
foreach ($GLOBALS['my_query_filters'] as $key => $name) {
$nested_query = array();
// continue if not found in url
if( empty($_GET[ $name ]) ) {
continue;
}
// get the value for this filter
// eg: http://www.website.com/events?city=melbourne,sydney
$values = explode(',', $_GET[ $name ]);
$nested_query['relation'] = 'OR';
foreach ($values as $value) {
$sub_query = array(
'key' => $name,
'value' => '"'.$value.'"',
'compare' => 'LIKE',
);
}
$meta_query[] = $nested_query;
}
// update meta query
$query->set('meta_query', $meta_query);
}
The topic ‘GET (url) parameters for data arrays?’ is closed to new replies.
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.