Home Forums ACF PRO Using compare IN operator for meta_query


Using compare IN operator for meta_query

  • I am using a custom post type with large amount of custom fields and I’ve created a listing page for the fields with the options of filtering and sorting. The taxonomy filters are working fine, I need help with ACF fields meta_query since the data is stored in serialized form I’m having trouble querying it with the compare ‘IN’ operator.

    I have already done quite a bit of research most of which suggested using multiple arrays for key/value combination. That approach is however not feasible since I have large amount of fields to use as filters.

    foreach($metaFilters as $key=>$metaFilter){
     $metaQuery[] = array(
     'key' => $key, 
     'value' => $metaFilter,
     'compare' => 'IN'

    This is how I’m using the meta_query for now which of course is not working. Please assist me with the issue.. if there’s no solution I’d love to hear the best approach available for this.

  • I have had the same issue in a query.


    ‘compare’ => ‘LIKE’

    Hope that helps…

  • You cannot use “IN” and must use “LIKE” as @acf-fan says and you need to have a nested query and do multiple “LIKE”s

    'meta_query' => array
        'relation' => 'OR',
          'key' => 'your-field',
          'value' => 'value 1',
          'compare' => 'LIKE'
          'key' => 'your-field',
          'value' => 'value 2',
          'compare' => 'LIKE'
          'key' => 'your-field',
          'value' => 'value 3',
          'compare' => 'LIKE'

    This can be costly to performance and it the list you are looking for is too long could even time out your site. This old post is about repeaters but it also applies in cases where ACF stores values as serialized arrays

  • Multiple “LIKE”‘s does not seem like a good choice for performance. I was thinking of going with two methods now

    #1 Creating a duplicate entry of metadata as array in the postmeta through ‘acf/update_value’

    #2 I was reading about creating a new field type

    I was wondering if there’s any way to save the checkboxes as an array instead of serialized data.

  • Serialized data is the way that WP stores arrays in the DB. It is not ACF that creates the serialized data, it is WP. What you are looking for, the thing that allows WP to us the “IN” comparison, is to have multiple DB entries that have the same meta_key.

  • I took an alternate solution for this using get_posts(), get_post_meta() to get the ids of my post type and the checkbox fields data and then comparing the meta with array_intersect() and in_array() for more accuracy.

    This works better than the ‘LIKE’ operator which was giving me unexpected results.

Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.