Home › Forums › Front-end Issues › Query: Multiple Checkbox Values (with LIKE) Crash › Reply To: Query: Multiple Checkbox Values (with LIKE) Crash
In case somebody faces the same problems as I did.
For getting the selection field and post its values as meta data:
// Credits: John Heubner
// create a function that will convert this repeater during the acf/save_post action
// priority of 20 to run after ACF is done saving the new values
add_filter('acf/save_post', 'convert_color_to_standard_wp_meta', 20);
function convert_color_to_standard_wp_meta($post_id) {
// pick a new meta_key to hold the values of the color field
// I generally name this field by suffixing _wp to the field name
// as this makes it easy for me to remember this field name
// also note, that this is not an ACF field and will not
// appear when editing posts, it is just a db field that we
// will use for searching
$meta_key = 'watch_functions';
// the next step is to delete any values already stored
// so that we can update it with new values
// and we don't need to worry about removing a value
// when it's deleted from the ACF repeater
delete_post_meta($post_id, $meta_key);
// create an array to hold values that are already added
// this way we won't add the same meta value more than once
// because having the same value to search and filter by
// would be pointless
$saved_values = array();
$functions = get_field('functies');
if(!empty($functions))
{
foreach($functions as $function)
{
// see if this value has already been saved
// note that I am using isset rather than in_array
// the reason for this is that isset is faster than in_array
if (isset($saved_values[$function])) {
// no need to save this one we already have it
continue;
}
// not already save, so add it using add_post_meta()
// note that we are using false for the 4th parameter
// this means that this meta key is not unique
// and can have more then one value
add_post_meta($post_id, $meta_key, $function, false);
// add it to the values we've already saved
$saved_values[$function] = $function;
} // end while have rows
} // end if have rows
} // end function
And quering the database.
$posts = get_posts(array(
'numberposts' => -1,
'post_type' => 'watch',
'posts_per_page' => 100,
'meta_key' => 'score',
'orderby' => 'meta_value',
'order' => 'DESC',
'meta_query' => array(
array(
'relation' => 'AND',
array(
'key' => 'watch_functions',
'compare' => '=',
'compare' => 'calorieenteller',
)
)
),
));
Cheers,
Martijn
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.