Support

Account

Home Forums General Issues Archive Filter with Multiple Values Select Field Reply To: Archive Filter with Multiple Values Select Field

  • Basically, on this page https://www.advancedcustomfields.com/resources/query-posts-custom-fields/ if you look in the section 3. Multiple custom field values (array based values) it tells you how to query based on these values. I understand it and my advice is this, ignore it and don’t do it. Even if you get it to work if you add too many “LIKE” queries for the same meta key you’ll just end up timing out your site because of the limits of WP_Query.

    Instead make your life easier. You know that you can use an “IN” query with normal WP post meta fields, you already have an example of this. So instead of trying to search the ACF field, which is difficult, convert the ACF field into something that is easy to search.

    
    add_filter('acf/save_post', 'convert_classes_to_standard_wp', 20);
    function convert_classes_to_standard_wp($post_id) {
      // use a different field name for your converted value
      $meta_key = 'converted_classes';
      // clear any previously stored values
      delete_post_meta($post_id, $meta_key);
      // get new acf value
      $values = get_field('classes', $post_id);
      if (is_array($values) && count($values) {
        foreach ($values as $value) {
          add_post_meta($post_id, $meta_key, $value, true);
        } // end foreach
      } // end if
    }
    

    Now you can use converted_classes instead of classes in your meta query with the 'compare' => 'IN'.